MySQL查询执行正常,但在使用!= NULL时返回(false)空结果集?

时间:2015-05-11 09:46:59

标签: mysql

我有以下结果集,我正在尝试向下钻取

+----+---------+---------------+---------------------+----------------------+---------------+-----------+------------------+------------------+
| id | auth_id | trusts_number | buy_sell_actions_id | corporate_actions_id | fx_actions_id | submitted |    created_at    |    updated_at    |
+----+---------+---------------+---------------------+----------------------+---------------+-----------+------------------+------------------+
|  2 |       6 | N100723       | 2                   | NULL                 | NULL          |         0 | 08/05/2015 11:30 | 08/05/2015 15:32 |
|  5 |       6 | N100723       | NULL                | NULL                 | 1             |         0 | 08/05/2015 15:10 | 08/05/2015 15:10 |
|  6 |       6 | N100723       | NULL                | NULL                 | 2             |         1 | 08/05/2015 15:12 | 08/05/2015 15:41 |
+----+---------+---------------+---------------------+----------------------+---------------+-----------+------------------+------------------+

使用查询

生成此结果集
SELECT * FROM actions WHERE auth_id = 6 AND trusts_number = 'N100723'

我也想摆脱fx_actions为NULL的任何字段,所以我将查询更改为

SELECT * FROM actions WHERE auth_id = 6 AND trusts_number = 'N100723' AND fx_actions_id != NULL

然而,这会返回一个空结果集。我以前从未在MySQL中使用过“负面”查询参数,所以我不确定它们是否应该采用不同的语法或什么?

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:4)

正常比较运算符与NULL不兼容。 Something = NULLSomething != NULL都将返回'unknown',这会导致在结果中省略该行。请改为使用特殊运算符IS NULLIS NOT NULL

SELECT * FROM actions 
WHERE auth_id = 6 
  AND trusts_number = 'N100723' 
  AND fx_actions_id IS NOT NULL

Wikipedia on NULL and its background

答案 1 :(得分:0)

由于null不是值,因此您应使用IS NOT NULL