在MySQL,我可以选择列只存在的东西吗?

例如,我有以下查询:

select phone, phone2
from jewishyellow.users
where phone like '813%'
and phone2

我试图只选择电话以813开始的行,电话2有一些东西在里面。


当前回答

select phone, phone2 from jewishyellow.users 
where phone like '813%' and phone2 is not null

其他回答

Use:

SELECT t.phone, 
       t.phone2 
  FROM jewishyellow.users t
 WHERE t.phone LIKE '813%' 
   AND t.phone2 IS NOT NULL

如果phone2字段中有空格,则可以使用IFNULL和TRIM函数忽略这些记录:

SELECT phone, phone2
FROM jewishyellow.users
WHERE phone LIKE '813%'
    AND TRIM(IFNULL(phone2,'')) <> '';

我们可以使用CASE将空白值设置为一些字符或字符串。 我使用NA作为默认字符串。

SELECT phone,   
CASE WHEN phone2 = '' THEN 'NA' END AS phone2 ELSE ISNULL(phone2,0) 
FROM jewishyellow.users  WHERE phone LIKE '813%'

令人惊讶的是(之前没有人提到过),我们发现下面的条件可以起作用:

WHERE ORD(field_to_check) > 0 

当我们需要排除空值和空值时。 有人知道这种方法的缺点吗?

检查NULL和空字符串值:

select phone
, phone2 
from users 
where phone like '813%' 
and trim(coalesce(phone2, '')) <>''

注意:我认为COALESCE()是SQL标准(-ish),而ISNULL()不是。