简体   繁体   中英

mysql query apply condition to where clause only if certain condition satisfied

I want to add condition_2 to mysql where clause by test condition_1. condition_1 is type = 'group:1' and condition_2 is username = 'rahim' . So, if type = 'group:1' satisfied, then I going to add username = 'rahim' in where clause. If not satisfied, I do not want to add this to where clause. I tried username = 'rahim' CASE WHEN type = 'group:1' . But, it output error:

[Err] 1064 : for the right syntax to use near 'CASE WHEN type = 'group:1' AND type = 'group:1''

 SELECT
    a.user_id,
    b.username,
    a.message,
    a.type
FROM
    livechat_message a
LEFT JOIN livechat_user b ON a.user_id = b.user_id
WHERE
    username = 'rahim' CASE
WHEN type = 'group:1'
AND type = 'group:1'

Thanks in advance.

你似乎想要:

WHERE NOT (type = 'group:1' AND username <> 'rahim')

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