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

Premlatha :

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.

Gordon Linoff :

You would seem to want:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27748&siteId=1
Recommended