我有一个问题,当我试图选择有一个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 * FROM 'column' WHERE 'column' IS NULL; 

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

跟着:

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

返回所有预期结果。

其他回答

还有一个<=>操作符:

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

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

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

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

有相同的问题查询:

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

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

跟着:

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

返回所有预期结果。

SELECT pid FROM planets WHERE userid is null;