Search code examples
mysqlsortingarray-reverse

MySQL: make record last in alpha sort


I can make a selected record appear at the beginning of a sorted query:

$sql = "SELECT meetID, meetingTitle
FROM meetings
ORDER BY meetingTitle = 'Other Meetings' DESC, meetingTitle DESC";

That forces "Other Meetings" to be at the top of an alphabetical array output. I want it to be at the bottom. I can then use array_reverse to place it at the end of the output array, but I am looking for a direct approach with the MySQL query. Is that possible?


Solution

  • Change ASC to DESC:

    $sql = "SELECT meetID, meetingTitle
            FROM meetings
            ORDER BY meetingTitle = 'Other Meetings' ASC, meetingTitle DESC";
    

    The value of meetingTitle = 'Other Meetings' is 1 for Other Meetings and 0 for other values. So if you want other values first, you want 0 to be ahead of 1, and that's ASCending order.