Background: 1, 2, 3, 4, 5 are required to display the ranking, but the default value of some data fields without ranking is 0, and there will be problems when using ASC directly
achieve effect
Implementation method: use MySQL's ORDER BY statement to achieve. The following is a sample SQL query statement
SELECT * FROM table_name
ORDER BY CASE WHEN column_name = 0 THEN 1 ELSE 0 END, column_name ASC;
In the above example, table_name is the table name and column_name is the column name to sort by.
Explain the meaning of the SQL statement:
CASE WHEN column_name = 0 THEN 1 ELSE 0 END: This is a conditional expression that returns 1 when the value of column_name is 0; otherwise, returns 0. This marks values of 0 as 1 and others as 0.
ORDER BY CASE WHEN column_name = 0 THEN 1 ELSE 0 END, column_name ASC: This is the sorting part. First sort according to the result of the previous conditional expression, that is, sort 0 to the end. Then sort in ascending order of column_name.
Through the above SQL query statement, you can achieve the effect of ranking 0 to the end and displaying other values in a positive order. You can use similar syntax in TP5's query builder to achieve the same effect.