user222427
user222427

Reputation:

mysql custom order excluding Values

i think this is really simple. I just want to return an select that has a custom order.

The result from my select is this.

  • Apples
  • Fruit
  • Berry
  • Mango
  • Home
  • Logout
  • I want the select to return this order

  • Home
  • Apples
  • Berry
  • Fruit
  • Mango
  • Logout
  • The ideal is that no matter the result Home is always first and Logout is always last. And everything in the middle is ordered asc. Any how would be fantastic!

    Upvotes: 1

    Views: 49

    Answers (3)

    Misha
    Misha

    Reputation: 571

    SELECT *
    FROM yourTable
    ORDER BY IF(name = 'home', -1, (IF(name = 'logout', 1, 0))), name
    

    Upvotes: 0

    Marty McVry
    Marty McVry

    Reputation: 2856

    SELECT *
    FROM yourTable
    ORDER BY CASE WHEN name = 'Home' THEN 1
                  WHEN name = 'Logout' THEN 3
             ELSE 2
             END ASC,
             name ASC
    

    Upvotes: 1

    Adriaan Stander
    Adriaan Stander

    Reputation: 166466

    How about

    SELECT *
    FROM MyTable
    ORDER BY  
        CASE
            WHEN ColumnValue = 'Home' THEN -1
            WHEN ColumnValue = 'Logout' THEN 1
            ELSE 0
        END,
        ColumnValue
    

    Upvotes: 0

    Related Questions