如何重建SQL查询,不会返回NULL

时间:2015-05-30 08:39:01

标签: mysql

我有SQL查询按条件获取行,外部查询计数行:

SELECT IdUserRecordstouser, SUM(IF(StatusRecordstouser IN('1', '0'), 1, 0)) AS NEW
FROM recordstouser
WHERE IdRecordstouser
IN (

SELECT IdRecordstouser
FROM recordstouser
WHERE IdtoUserRecordstouser =544
)
AND StatusRecordstouser = 1

问题是,如果StatusRecordstouser = 0我得到NULL字段:IdUserRecordstouser, NEW,但我需要在任何情况下显示行,因为存在IdtoUserRecordstouser =544的行但StatusRecordstouser = 0 < / p>

1 个答案:

答案 0 :(得分:0)

如果您想选择那些OR而不管StatusRecordtouser = 1的记录,为什么不在过滤器中使用IdtoUserRercordstouser? (那是你想做的吗?)

如上所述,您的子查询将返回空集({NULL})或单件集({544}),具体取决于表recordstouser是否具有id的记录= {54} AND运算符会使StatusRecordstouser = 0true AND false的结果为false复杂化。

另外,您应该将列重命名为更简洁。