MYSQL - ORDER BY FIELD encounters null to sort

Null fields cannot be recognized in normal sorting

Wrong approach

null cannot directly participate in sorting

ORDER BY FIELD( proccess, '1', null, '3', '4', '5', '6', '7' )

You will find that null cannot participate in sorting and will be ranked first. So how to specify its position?

Solution

Use the COALESCE function to convert null to a specific value.

COALESCE function: determine the input parameters from left to right and return the first non-null value

ORDER BY FIELD( COALESCE(proccess,'null'), '1', 'null', '3', '4', '5', '6', '7' )

Guess you like

Origin blog.csdn.net/ZHAI_KE/article/details/125913311