我有一个问题,当我试图选择有一个NULL为某列的行,它返回一个空集。然而,当我在phpMyAdmin中查看表时,它对大多数行表示为空。

我的问题是这样的:

SELECT pid FROM planets WHERE userid = NULL

每次都是空集。

A lot of places said to make sure it's not stored as "NULL" or "null" instead of an actual value, and one said to try looking for just a space (userid = ' ') but none of these have worked. There was a suggestion to not use MyISAM and use innoDB because MyISAM has trouble storing null. I switched the table to innoDB but now I feel like the problem may be that it still isn't actually null because of the way it might convert it. I'd like to do this without having to recreate the table as innoDB or anything else, but if I have to, I can certainly try that.


当前回答

还有一个<=>操作符:

SELECT pid FROM planets WHERE userid <=> NULL

是可行的。好的事情是<=>也可以用于非null值:

SELECT NULL <=> NULL返回1。

SELECT 42 <=> 42的结果也是1。

请看这里:https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_equal-to

其他回答

SELECT pid FROM planets WHERE userid IS NULL

有相同的问题查询:

SELECT * FROM 'column' WHERE 'column' IS NULL; 

没有返回值。 MyISAM似乎有问题,在InnoDB中对数据的相同查询返回了预期的结果。

跟着:

SELECT * FROM 'column' WHERE 'column' = ' '; 

返回所有预期结果。

SQL NULL是特殊的,你必须做WHERE field IS NULL,因为NULL不能等于任何东西,

包括自身(即:NULL = NULL总是假的)。

参见规则3 https://en.wikipedia.org/wiki/Codd%27s_12_rules

SELECT pid FROM planets WHERE userid is null;

信息来自http://w3schools.com/sql/sql_null_values.asp:

1)空值表示缺失未知数据。 2)默认情况下,表列可以保存NULL值。 3) NULL值与其他值区别对待 4)不可能比较NULL和0;它们不相等。 5)不可能通过比较来测试NULL值 操作符,如=、<或<>。 6)我们将不得不使用IS NULL和IS NOT NULL操作符代替

所以如果你遇到了问题

SELECT pid FROM planets WHERE userid IS NULL