Select Query to check both or either or condition

安稳与你 提交于 2019-12-02 08:12:31

Assuming that when you mean "not present", mailid or phonenumber will be NULL in database,

SELECT userid, mailid, phonenumber 
FROM muser 
WHERE (phonenumber = ? AND (mailid IS NULL OR mailid = ''))
OR ((phonenumber IS NULL OR phonenumber = '') AND mailid = ?)
OR (phonenumber = ? AND mailid = ?)

When I'm dealing with filter where the value can be any including null I'll try add coalesce()

SELECT userid, mailid, phonenumber 
FROM muser 
WHERE coalesce(phonenumber,'no data') = coalesce(?,'no data') 
OR coalesce(mailid,'no data') = coalesce(?, 'no data');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!