我的查询非常简单:
SELECT * FROM `all_conversations` WHERE `deleted_1` != '1';
我的deleted_1
默认为null
或某个用户ID,但由于某种原因,此查询总是返回0行,我也尝试<>
但仍然没有运气可能是什么错?
EDTI 因此,在运行更多查询后,我发现我的问题是deleted_1
字段的默认值,它是NULL
所以我修改了我的查询,现在它工作正常:
SELECT *
FROM `all_conversations`
WHERE `deleted_1` != 'NULL'
AND `deleted_1` != 23
答案 0 :(得分:71)
SELECT * FROM all_conversations WHERE deleted_1 <> 1 OR deleted_1 IS NULL
NULL值需要特殊处理:http://dev.mysql.com/doc/refman/5.1/en/working-with-null.html
我建议使用菱形运算符(<>
)支持!=
,因为第一个是有效的SQL,第二个是MySQL的添加。
答案 1 :(得分:5)
我建议使用NULL-safe运算符和否定
SELECT * FROM `all_conversations` WHERE NOT(`deleted_1` <=> '1');
答案 2 :(得分:4)
你能试试这个:deleted_1 is not null and deleted_1 != '1'
吗?
mysql> select 0 is not null and 0 != '1', 1 is not null and 1 != '1', null is not null and null != '1';
+----------------------------+----------------------------+----------------------------------+
| 0 is not null and 0 != '1' | 1 is not null and 1 != '1' | null is not null and null != '1' |
+----------------------------+----------------------------+----------------------------------+
| 1 | 0 | 0 |
+----------------------------+----------------------------+----------------------------------+
或deleted_1 is null or deleted_1 != '1'
:
mysql> select 0 is null or 0 != '1', 1 is null or 1 != '1', null is null or null != '1';
+-----------------------+-----------------------+-----------------------------+
| 0 is null or 0 != '1' | 1 is null or 1 != '1' | null is null or null != '1' |
+-----------------------+-----------------------+-----------------------------+
| 1 | 0 | 1 |
+-----------------------+-----------------------+-----------------------------+
这实际上取决于你想要回来的东西。
答案 3 :(得分:2)
试试这个..希望它会对你有用
SELECT *
FROM `all_conversations`
WHERE `deleted_1` IS NOT NULL
AND `deleted_1` <> 23
答案 4 :(得分:0)
我同意以上答案,如果您有多个AND条件,只需添加其他详细信息 像下面的查询一样,您应该将要检查的列与Where条件的第一部分不等于或为null,因为OR部分会使您的条件不正确
SELECT ts.*, ct.Name AS CategoryName
FROM MJ.Tasks AS ts
LEFT JOIN MJ.Lookup_Category AS ct ON ts.CategoryID = ct.ID
WHERE ts.Status!=1 OR ts.Status IS NULL AND ts.CategoryID = @CategoryID AND ts.UserID = @UserID AND CAST(ts.EndDate AS DATE) = CAST(@EndDate AS DATE)
答案 5 :(得分:-2)
如何删除1?
周围的单引号SELECT * FROM `all_conversations` WHERE `deleted_1` != 1;