如何检查列值是否为null然后使用sql查询检查下一列

时间:2015-04-07 17:59:45

标签: mysql sql

SELECT users.group_id, testtb.id 
FROM users 
LEFT JOIN testtb on users.group_id =testtb.id 
where testtb.id is not null ORDER BY users.group_id

在我的上述查询中,如果where

,我想再使用一个where testtb.name is not null条件(即testtb.id is not null

3 个答案:

答案 0 :(得分:0)

您可以使用AND子句

    SELECT users.group_id, testtb.id FROM users 
    LEFT JOIN testtb on users.group_id =testtb.id 
    AND testtb.id is not null 
AND testtb.name is not null
    ORDER BY users.group_id;

答案 1 :(得分:0)

SELECT users.group_id, testtb.id 
FROM users 
LEFT OUTER JOIN testtb on users.group_id =testtb.id 
where testtb.id is not null 
and testtb.name is not null
ORDER BY users.group_id

答案 2 :(得分:0)

您可以使用coalesce

SELECT users.group_id, testtb.id 
FROM users 
LEFT JOIN testtb on users.group_id =testtb.id 
where testtb.id is not null 
AND COALESCE(testtb.name1,testtb.name2,testtb.name3) IS NOT NULL
ORDER BY users.group_id