如何检索没有空字符串值和NULL值的mysql不同值
SELECT
DISTINCT CON.EMAILADDRESS AS 'E-MAIL'
FROM STUDENT
INNER JOIN CONTACT CON ON STUDENT.CONTACT_ID = CON.ID
WHERE
(CON.EMAILADDRESS IS NOT NULL
OR CON.EMAILADDRESS !=' ');
但是在输出中仍然会收到空的电子邮件。无法弄清楚我在做什么。
答案 0 :(得分:2)
试试这个:
SELECT
DISTINCT CON.EMAILADDRESS AS 'E-MAIL'
FROM STUDENT AST
INNER JOIN CONTACT CON ON AST.CONTACT_ID = CON.ID
WHERE length(trim(CON.EMAILADDRESS)) >0 and CON.EMAILADDRESS IS NOT NULL
答案 1 :(得分:2)
您唯一的问题是您使用OR代替AND。
让我们看一下值为NULL的情况:
CON.EMAILADDRESS IS NOT NULL
=> FALSE CON.EMAILADDRESS != ' '
=> NULL FALSE OR NULL =>空值。由于标准不会导致TRUE,因此您不能选择NULL。
如果该值为空字符串'',' '或任何长度:
CON.EMAILADDRESS IS NOT NULL
=> TRUE CON.EMAILADDRESS != ' '
=> FALSE TRUE或FALSE =>真正。您选择空字符串。
我认为这让你很困惑:尽管错误地使用了OR而不是AND,你仍然删除了一些空字符串,但不是全部。
所以:
WHERE CON.EMAILADDRESS IS NOT NULL AND CON.EMAILADDRESS != ' ';
或者,因为任何字符串!= ''
都不能为NULL(NULL != ''
=> NULL,而不是TRUE),只需:
WHERE CON.EMAILADDRESS != '';
答案 2 :(得分:1)
AND
SELECT DISTINCT CON.EMAILADDRESS AS 'E-MAIL'
FROM STUDENT AST
INNER JOIN CONTACT CON ON AST.CONTACT_ID = CON.ID
WHERE CON.EMAILADDRESS IS NOT NULL
AND CON.EMAILADDRESS !=' '
AND CON.EMAILADDRESS !='';