A
A
Alexey Burlaka2017-05-29 17:58:31
MySQL
Alexey Burlaka, 2017-05-29 17:58:31

How to compose a mysql query specifying a priority group by ID list?

Good afternoon!
Please help me with MYSQL query.
I am performing the task: to implement a search simultaneously...
- by article (item property - table associated with the element),
- by name (table field),
- by brands (element property),
- by belonging to collections (element property),
- by the name of the sections (an element of another table, to which the element is linked by the SECTION_ID field).
At the same time, to form an array of results, divided into pagination.
The priority of submitting search results is according to the list above. That is, first we show the hit by article, then by name, ... and so on through the list
. Many related tables are involved in compiling the result, I want to select these links only for those elements that hit a particular page in pagination. To optimize queries, I reduce everything to one complex query.
How I do it now:
- iterate through the conditions (search by article, by name, by brand...),
- accumulate an array of IDs of those elements that match each of the parameters,
- in the final request, pass the accumulated array of ID values ​​as a constraint, select records with all the desired properties in the range according to the pagination.
Selection and page works, except that the priority is not respected. Goods in the sample are "mixed".
Tell me how to make a request?
At least at the mysql level (because it is at this level that there is no understanding right now).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2017-05-29
@AlexeyGfi

SELECT * FROM T WHERE T.`артикул` = a
UNION DISTINCT
SELECT * FROM T WHERE T.`название` = n
UNION DISTINCT
SELECT * FROM T WHERE T.`бренд` = b;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question