How to sort the result of a query by a column where there is a value in another column in the same table?

0

I have a table with several columns, and I want to sort the results by the values of one of the columns where another column in the same table is equal to a value.

I have already tried to implement the query with CASE , IF's and FIELD() .

I want the results sorted by tech.value when tech.id_color = 5

Here is the query:

SELECT DISTINCT * 
FROM products, tech_products 
WHERE products.idsub = 1 AND 
    tech_products.id = products.id 
ORDER by tech.id_cor = 5 AND tech.value ASC
    
asked by anonymous 18.05.2018 / 16:52

1 answer

1
SELECT DISTINCT * 
FROM products, tech_products 
WHERE products.idsub = 1 AND 
    tech_products.id = products.id 
ORDER by (case when (tech.id_cor = 5) then 0 else 1 end) ASC,
         tech.id_cor

The syntax of CASE may vary depending on the DB, but I think it works out.

    
18.05.2018 / 17:13