Home > Mobile >  Order by value decrement to one mysql
Order by value decrement to one mysql

Time:03-23

Ex : I have V001, V002, V003, V004 records,

     tid        is_premium
    ----       ----------
    V001         0
    V002         0
    V003         0 
    V004         1
    V005         1
    

How to get records order by below like this,

      V001
      V004
      V005
      V002
      V003

2nd Ex : I have V006, V007, V008, V009 records,

     tid        is_premium
    ----       ----------
    V006         0
    V007         0
    V008         1 
    V009         0

    

How to get records order by below like this,

      V006
      V008
      V007
      V009
   

I want to above order in MySQL, I have to write but not possible, I tried both multiple column order by using Mysql, but I am not getting correct response. Can anyone help in this., I want to above order in MySQL,

CodePudding user response:

Well, what this query does is to sort it in descending order by is_premium and then displays it simply that way

SELECT tid FROM CLIENTE ORDER BY is_premium DESC;

Note : Where it says table put your table

I put a link with the test code for you to try it. : https://www.db-fiddle.com/f/3PnzHErrf2fZFGZY67K12X/55

CodePudding user response:

Probably, @Chinnu the most efficient way to do what you need is get all from your DB and do some logic in your backend instead use mysql-query. Once you have an object with everything, you can apply conditions that will make sure if some patient is already in the queue and if that patient is premium or not and so on.

(SELECT * FROM Schema.records LIMIT 1)
UNION
(SELECT * FROM Schema.records ORDER BY `is_premium` DESC LIMIT 0, 1000);

Picture of the example working

That should resolve your problem but it will put all is_premium = 1 before the others keeping the first row intact.

  • This query is specific to what you asking for.

Another way could be:

SELECT * FROM Schema.records ORDER BY `tid`='V001' DESC, `is_premium` DESC;
  • Related