In MySQL5.7, I have a column having 28 columns and 4000 rows. I want to bring rows having data for all or rows having data for maximum columns first and then for the other rows to progressively follow. How do I achieve this ?
Eg:
Table : Student
name | age | address |
---|---|---|
tan | 10 | |
gib | 10 | california |
hal | ||
pur | 12 |
Expected Output: ‘gib’ should come first, followed by either ‘tan’ or ‘pur’ since they both have data for only 1 column, followed by ‘hal’ which doesn’t have any data.
2
Answers
Assuming the
name
column would never have null values, we can try:Use this
ORDER BY
clause:Each of the
columnX IS NOT NULL
expressions evaluates to1
forTRUE
or0
forFALSE
.What you get by summing them is the number of non–
NULL
columns in each row and you can use this numeric result to sort the rows descending.