V
V
Viktor Vsk2014-07-14 15:39:30
MySQL
Viktor Vsk, 2014-07-14 15:39:30

How to sort objects so that some of them (by condition) are always at the end?

There are goods. Products have prices. Products can be sorted according to different conditions. Output page by page.
How to make it so that those goods whose price is equal to zero are always at the end?
For some time, no working solutions came to mind, except how to collect an array and display the array page by page.
But it seems that it should be possible to do this at the sampling level as well.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Entelis, 2014-07-14
@DmitriyEntelis

Option 1

SELECT .... WHERE price > 0 AND ... ORDER BY ...
UNION
SELECT .... WHERE price = 0 AND ... ORDER BY ...

Option 2
Option 3
Similar to option 2, but store has_price explicitly in the database
PS I think that the 3rd one is the most correct

A
Alexey Bespalov, 2014-07-14
@nulldef

SELECT * FROM goods .... ORDER BY <ваша выборка>, price DESC

A
AxisPod, 2014-07-14
@AxisPod

Add a field indicating the presence of a price, because != 0 will be very bad, the index does not work here. Take a solution from DmitriyEntelis and add a check for this field in the 1st option in the 1st request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question