Hello. Interested in such a question. There is a table with the following structure: name, parent, price, etc. The task: first to display in the table values ​​with certain values ​​of parent (for example, 1,2,3), and then values ​​with all other values ​​of parent (ie, 4, 5, 6 ..) It should look something like this :

table I tried to make a request in the following way, but did not give any result

SELECT * FROM `modx_site_content` ORDER BY parent IN (54721, 55450, 54747) ASC 

MariaDB engine Tell me, please, what to fix or how to make a request correctly ...

  • Why asc something? DESC .. - Akina
  • When changing ASC to DESC, nothing has changed. And don't these values ​​just answer the sort order? - Yumie

2 answers 2

 SELECT * FROM `modx_site_content` ORDER BY case when parent IN (54721, 55450, 54747) then 0 else 1 end, parent 
     SELECT * FROM `modx_site_content` ORDER BY parent ASC 

    or

     SELECT * FROM `modx_site_content` ORDER BY 2 

    If you need a filter for values,

     SELECT * FROM `modx_site_content` WHERE parent IN (54721, 55450, 54747) ORDER BY parent ASC SELECT * FROM 'modx_site_content' WHERE parent IN (54721, 55450, 54747) UNION ALL SELECT * FROM 'modx_site_content' WHERE parent NOT IN (54721, 55450, 54747) 
    • The first option is not suitable, because the values ​​are quite different (i.e. let's say it’s necessary to select the values ​​first with parent = 54731, 547277, and then the rest. The second option will simply generate an error 1054, because we are accessing a nonexistent column ... - Yumie
    • @Yumie, updated the post - DNS
    • The third option, displays only the values ​​that we specify in IN. But it is necessary that he deduced everything else after these values ​​... - Yumie
    • Then break the query and use UNION : SELECT * FROM 'modx_site_content' WHERE parent IN (54721, 55450, 54747) UNION SELECT * FROM 'modx_site_content' WHERE parent NOT IN (54721, 55450, 54747) - DNS
    • And so I tried. The reason is that the combined queries must have the same number of values. Those. in the first one if it is 50, and in the second 500 it will give an error. - Yumie