If else on WHERE clause

前端 未结 5 1850
栀梦
栀梦 2020-12-09 01:53

I\'ve this query:

SELECT  `id` ,  `naam` 
FROM  `klanten` 
WHERE (
`email` LIKE  \'%@domain.nl%\'
OR  `email2` LIKE  \'%@domain.nl%\'
)

But

5条回答
  •  天涯浪人
    2020-12-09 02:29

    Note the following is functionally different to Gordon Linoff's answer. His answer assumes that you want to use email2 if email is NULL. Mine assumes you want to use email2 if email is an empty-string. The correct answer will depend on your database (or you could perform a NULL check and an empty-string check - it all depends on what is appropriate for your database design).

    SELECT  `id` ,  `naam` 
    FROM  `klanten` 
    WHERE `email` LIKE  '%anja@fiskkoer.nl%'
    OR (LENGTH(email) = 0 AND `email2` LIKE  '%anja@fiskkoer.nl%')
    

提交回复
热议问题