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:选择列为空的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)