There is a database type:

field1 price 1 вариант 500 2 вариант 600 1 вариант 700 3 вариант 800 

On the site (product catalog), the data is grouped by the field1 column, and the price column is pulled out as <select> separate mysql query (without grouping)

How to make data sorted by price and field1 column, like this (correct only):

 ORDER by `price` where `field1` = '1 вариант' ASC 

Options using SELECT * FROM table WHERE field = '1 вариант' not suitable, because The request is automatically generated by the code depending on the configured filters, and pulls out the data of all the cells for further work.

  • and what in ORDER by already where it is possible to write? - pavel
  • Does the phrase "just right" mean something to you? So it was easier for me to explain the task. - f1amestar
  • if I understand correctly, it should be like this: SELECT * FROM table WHERE field = '1 option' ORDER BY price, field ASC - Artem Konovalov
  • Blame forgot to clarify that this option is not possible, because the sample in this query is generated automatically depending on the filters, it is only possible to change the ORDER by. - f1amestar
  • in order by you you will not be able to stuff where - Artem Konovalov

1 answer 1

You can do the following:

 SELECT * FROM tbl ORDER BY IF(`field1` = '1 вариант', 0, 1), `price` 

Split the result into two groups using the calculated IF( expression IF( field1 = '1 вариант', 0, 1) , the group '1 option' goes first, then everything else. Sorting by the second column will result in sorting within groups by price.

  • Is it possible to use 2 options with IF? - f1amestar
  • If you need to build more options, you will probably be more comfortable with CASE, although you can also use IF. - cheops
  • In order for the task to be clear: in field1 the available size of the trading position is indicated, there are from 2 to 4 in total (let's call them Variant 1, Variant 2, Variant 3, Variant 4) and we need to sort them out by a smaller one, i.e. either this is "1 option" or it is "2 option", because one of them is 100% present, please tell me how, for it is not strong in ORDER by - f1amestar
  • @ f1amestar and how is a minority of options set? By the number of entries in them or something else? - cheops
  • by name, this is a boutique of bed linen, where there is a 1.5 bed room, where it is 2 bed, where is the Euro, and where is the Family, and where this size is, and where it is not. - f1amestar