Skip to content
Advertisement

Mysql multiple order, one order by date with condition

I have a simple SQL request which orders the result by the number of sales and then by the number of views.

But I would like the new images with a date greater than for example “2022-05-01” to appear just after the ones that have been sold.

Is it possible to have multiple order and in one of this order a condition?

Example of current results:

Let’s say we have 2 new images with a date >= 2022-05-01 with low viewcount and no sales, and I would like:

Advertisement

Answer

You can use the boolean expression sales = 0 AND date >= '2022-05-01' in the ORDER BY clause, between the 2 columns:

See the demo.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement