-1

If there are 3 records with different amounts as 500, 1000 and 1500 respectively, so for default query will be like amount order by DESC, 2nd condition is there is a button like "MOVE TO TOP", if 500 amount user click on button then that record move to top and next records should be display amount wise like 1500, 1000.

If click on "move to top" button then current date will stored in click_date column I am using just default amount DESC query,

move_top table

id         name       amount      clickdate
----------------------------------------------------   
1          test       500        2017-02-04 07:25:00
2          test2     1000        2017-02-02 11:18:42
3          test3     1500        2017-02-01 03:26:07

Query:

SELECT * 
FROM `move_top`  
ORDER BY clickdate DESC, amount DESC

but it's not working properly

6
  • 1
    I have seen this question already today... Commented Feb 4, 2017 at 10:53
  • When you use ORDER BY x, y, it means to order by x, but if two rows have the same x then you order them by y.
    – Barmar
    Commented Feb 4, 2017 at 10:53
  • 1
    Since all your rows have different dates, the second condition in ORDER BY has no effect. I don't understand what you're trying to do.
    – Barmar
    Commented Feb 4, 2017 at 10:55
  • Please make a results example Commented Feb 4, 2017 at 10:58
  • Hey show us what you what your end goal is meant to be and we can understand what you need to do :)
    – danjbh
    Commented Feb 4, 2017 at 10:59

1 Answer 1

0

Unhapply mysql will not "understand" the logic you need. If you put ORDER BY clickdate DESC, amount DESC, it will order first by clickdate, and if the clickdate are the same then it will order by amount.

A workarround to the result you want(When user clicks MOVE TO THE TOP ):

 SELECT * FROM `move_top`  WHERE id = {id_that_user_have_clicked}
 UNION 
 SELECT * FROM `move_top`  ORDER BY amount DESC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.