MySQL:选择列为空的行

MySQL: selecting rows where a column is null(MySQL:选择列为空的行)
本文介绍了MySQL:选择列为空的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我遇到了一个问题,当我尝试为某个列选择具有 NULL 的行时,它返回一个空集.但是,当我查看 phpMyAdmin 中的表格时,大多数行都显示为 null.

I'm having a problem where when I try to select the rows that have a NULL for a certain column, it returns an empty set. However, when I look at the table in phpMyAdmin, it says null for most of the rows.

我的查询如下所示:

SELECT pid FROM planets WHERE userid = NULL

每次都是空集.

很多地方说要确保它不会存储为NULL"或null"而不是实际值,还有一个说尝试只查找一个空格 (userid = ' ') 但这些都没有奏效.有人建议不要使用 MyISAM 而使用 innoDB,因为 MyISAM 无法存储 null.我将表切换到 innoDB,但现在我觉得问题可能在于它实际上仍然不是 null,因为它可能转换它的方式.我想这样做而不必将表重新创建为 innoDB 或其他任何东西,但如果必须,我当然可以尝试.

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.

推荐答案

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

SQL NULL's special, and you have to do WHERE field IS NULL, as NULL cannot be equal to anything,

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

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

这篇关于MySQL:选择列为空的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Can#39;t Create Entity Data Model - using MySql and EF6(无法创建实体数据模型 - 使用 MySql 和 EF6)
MySQL select with CONCAT condition(MySQL选择与CONCAT条件)
Capitalize first letter of each word, in existing table(将现有表格中每个单词的首字母大写)
How to retrieve SQL result column value using column name in Python?(如何在 Python 中使用列名检索 SQL 结果列值?)
Update row with data from another row in the same table(使用同一表中另一行的数据更新行)
Exporting results of a Mysql query to excel?(将 Mysql 查询的结果导出到 excel?)