parboy
parboy

Reputation: 67

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?

Upvotes: 0

Views: 25

Answers (2)

Barmar
Barmar

Reputation: 780843

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.

Upvotes: 2

Oto Shavadze
Oto Shavadze

Reputation: 42753

    SELECT meetID, meetingTitle FROM  meetings
    ORDER BY 
    CASE WHEN meetingTitle = 'Other Meetings' THEN 2 ELSE 1 END, meetingTitle DESC

Upvotes: 0

Related Questions