在MySQL,我可以选择列只存在的东西吗?
例如,我有以下查询:
select phone, phone2
from jewishyellow.users
where phone like '813%'
and phone2
我试图只选择电话以813开始的行,电话2有一些东西在里面。
在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 <> "");
可能需要一些调整,这取决于您的默认值是什么。如果你允许Null填充,那么你可以做“Not Null”代替,这显然更好。
其他回答
SELECT phone, phone2
FROM jewishyellow.users
WHERE phone like '813%' and (phone2 <> "");
可能需要一些调整,这取决于您的默认值是什么。如果你允许Null填充,那么你可以做“Not Null”代替,这显然更好。
检查NULL和空字符串值:
select phone
, phone2
from users
where phone like '813%'
and trim(coalesce(phone2, '')) <>''
注意:我认为COALESCE()是SQL标准(-ish),而ISNULL()不是。
select phone, phone2 from jewishyellow.users
where phone like '813%' and phone2 is not null
你可以使用like操作符通配符来实现:
SELECT t.phone,
t.phone2
FROM jewishyellow.users t
WHERE t.phone LIKE '813%'
AND t.phone2 like '[0-9]';
通过这种方式,您可以获得所有具有数字前缀的phone2。
Use:
SELECT t.phone,
t.phone2
FROM jewishyellow.users t
WHERE t.phone LIKE '813%'
AND t.phone2 IS NOT NULL