Select Query to check both or either or condition

后端 未结 2 1565
栀梦
栀梦 2021-01-26 05:21

I\'m using postgresql 9.1 and wish to select a single record from table. Details are as below :

table name : muser

fields present in table : userid,username,fir

相关标签:
2条回答
  • 2021-01-26 05:42

    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');
    
    0 讨论(0)
  • 2021-01-26 05:57

    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 = ?)
    
    0 讨论(0)
提交回复
热议问题