如果 WHERE 子句上的其他

2021-12-12 00:00:00 sql if-statement mysql

我有这个查询:

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

但我想做这样的事情:

SELECT  `id` ,  `naam` 
FROM  `klanten` 
WHERE IF(`email` > 0,
`email` LIKE  '%@domain.nl%'
,  `email2` LIKE  '%@domain.nl%'
)

如何检查电子邮件是否存在?我想使用电子邮件,如果此字段为空,我想使用 email2.我该如何实现?

How to check if email exist? I want to use email and if this field is empty I want to use email2. How do I accomplish this?

推荐答案

IF 用于选择字段,然后将 LIKE 子句放在其后面:

IF is used to select the field, then the LIKE clause is placed after it:

SELECT  `id` ,  `naam` 
FROM  `klanten` 
WHERE IF(`email` != '', `email`, `email2`) LIKE  '%@domain.nl%'

相关文章