Really I don't know how to say it as question, but with example it will be clear to everyone, I have data retrieved from MYSQL database sorted by one of columns , my question if one of those values in this column is equal, I need to use another column to set who is show first.
col1 - col2
10 - 100
20 - 120
20 - 140
30 - 90
see here value 20 mentioned twice so I need to show the 20 with 140 before the 20 with 120 using MYSQL Query.
If the values are the same in the column used in the order by, the results will be random.
You can also ORDER BY two or more columns, which creates a nested sort . The default is still ascending, and the column that is listed first in the ORDER BY clause takes precedence. The following query and Figure 3 and the corresponding query results show nested sorts.
Using Group By and Order By Together When combining the Group By and Order By clauses, it is important to bear in mind that, in terms of placement within a SELECT statement: The GROUP BY clause is placed after the WHERE clause. The GROUP BY clause is placed before the ORDER BY clause.
Yes, you can order by a field(s)even if it is not your in your select statement but exists in your table.
SELECT
*
FROM
MyTable
ORDER BY
Col1,
Col2 DESC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With