简体   繁体   中英

Mysql case statement not working

My database version 5.6.34

I am using case statement like

SELECT 
    customerName, state, country
FROM
    customers
ORDER BY (CASE
    WHEN state IS NULL THEN country
    ELSE state
END);

table name is customers

I am writting this query in mysql database under SQL, i also attached my table you can see there,

The issue is

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'state IS NULL THEN country     ELSE state END) LIMIT 0, 25' at line 6

it will be really grateful for me if anybody help me please

Just remove that ()

SELECT 
    customerName, state, country
FROM
    customers
ORDER BY CASE
    WHEN state IS NULL THEN country
    ELSE state
END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM