简体   繁体   English

在SELECT中使用时,CASE'NOT NULL'的mysql语法

[英]mysql syntax for CASE 'NOT NULL' when used within SELECT

I am trying to use CASE 'NOT NULL' within a SELECT statement but I keeping getting this error message: 我试图在SELECT语句中使用CASE'NOT 'NOT NULL' ,但我一直收到以下错误消息:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax 查看与您的MySQL服务器版本相对应的手册以获取正确的语法

SELECT 
        u.id,
                CASE  ph.thumbsize WHEN NULL THEN ph.thumbsize ELSE 'defaultpicture.jpg'

            FROM
            user u  
            LEFT OUTER JOIN
                list_photos ph
        ON 
                u.id = ph.userId;

I have also tried these different formats for CASE : 我也为CASE尝试了这些不同的格式:

(CASE WHEN ph.thumbsize IS NOT NULL THEN 'ph.thumbsize' ELSE
 'defaultpicture.jpg')as picture       

           CASE WHEN ph.thumbsize IS NOT NULL THEN ph.thumbsize ELSE defaultpicture.jpg

You're missing an END , among other minor issues. 您错过了END和其他一些小问题。 Try: 尝试:

SELECT 
    u.id,
    CASE WHEN (ph.thumbsize IS NULL) THEN 
        'defaultpicture.jpg'
    ELSE
        ph.thumbsize
    END AS picture
FROM
    user u  
LEFT OUTER JOIN
    list_photos ph
ON 
    u.id = ph.userId;

try this: 尝试这个:

SELECT 
    u.id,
            CASE  ph.thumbsize WHEN NULL THEN ph.thumbsize ELSE 'defaultpicture.jpg' END

        FROM
        user u  
        LEFT OUTER JOIN
            list_photos ph
    ON 
            u.id = ph.userId;

In CASE you need to close it by END 在CASE中,您需要在END之前关闭它

Why not using IFNULL : 为什么不使用IFNULL

..., IFNULL(ph.thumbsize,'defaultpicture.jpg') AS picture, ... ...,IFNULL(ph.thumbsize,'defaultpicture.jpg')AS图片,...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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