In general, there is a table {table} , in it there are columns {id, price} , for some products price = 0 , which means that the price is negotiable. If I make a SQL query with ORDER BY price ASC then the products with the contract price are displayed first, but I need to remove them to the end. So, is it possible to implement this using an SQL query?

PySy, I know that it is possible to re-sort the output lines with php foreach , where to track price == 0 and transfer it to the end of the array, BUT the site is written very crutch, very crooked, yii is used somewhere, somewhere one version, somewhere else, there are shorter legs than a leg and I want to do with a little blood.

    2 answers 2

    In the sorting conditions, you can use the case ..when .. then ..

     select id, price from table order by case when price = 0 then null else price end asc 

    In this example, I rely on the fact that in my database, when sorting, null will always be at the end. If this is not the case in your database, then null can be replaced with a very large number.

    • thanks, it worked just with then 99999999 - Peresada
     SELECT * FROM table ORDER BY price = 0, price 
    • And in which DBMS can you write and how should it work? In my Oracle it is impossible not to write like this ( - Viktorov
    • @lDrakonl in which DBMS you can write to MySQL, for example. 90% for the fact that the author is he or his clone. how should it work? Yes, just a boolean result is converted to integer - 1 for true and 0 for false. Here are those records for which it is true, and drown. - Akina
    • Interesting, thanks! - Viktorov