MySQL 只选择非空值
是否可以执行仅采用 NOT NULL 值的 select 语句?
现在我正在使用这个:
SELECT * FROM table
然后我必须使用 php 循环过滤掉空值。
有没有办法做到:
SELECT * (that are NOT NULL) FROM table
?
现在,当我选择 * 时,我得到 val1,val2,val3,null,val4,val5,null,null 等...但我只想获取结果中不为 null 的值。如果不使用循环进行过滤,这可能吗?
Is it possible to do a select statement that takes only NOT NULL values?
Right now I am using this:
SELECT * FROM table
And then I have to filter out the null values with a php loop.
Is there a way to do:
SELECT * (that are NOT NULL) FROM table
?
Right now when I select * I get val1,val2,val3,null,val4,val5,null,null etc.... but I just want to get the values that are not null in my result. Is this possible without filtering with a loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您应该使用
IS NOT NULL
。 (比较运算符=
和<>
都在表达式两侧给出UNKNOWN
和NULL
。 )为了完整起见,我会提到在 MySQL 中您还可以否定 null 安全相等运算符 但这不是标准 SQL。
编辑以反映评论。听起来您的表可能不是第一范式,在这种情况下更改结构可能会使您的任务更容易。不过还有其他几种方法...
上述方法的缺点是它会为每列多次扫描表一次。下面可能可以避免这种情况,但我还没有在 MySQL 中测试过这一点。
You should use
IS NOT NULL
. (The comparison operators=
and<>
both giveUNKNOWN
withNULL
on either side of the expression.)Just for completeness I'll mention that in MySQL you can also negate the null safe equality operator but this is not standard SQL.
Edited to reflect comments. It sounds like your table may not be in first normal form in which case changing the structure may make your task easier. A couple of other ways of doing it though...
The disadvantage of the above is that it scans the table multiple times once for each column. That may possibly be avoided by the below but I haven't tested this in MySQL.
您可以过滤掉特定列中包含 NULL 值的行:
如果您想过滤掉任何列中包含 NULL 值的行,请尝试以下操作:
更新:根据您的评论,也许您想要这个?
我同意马丁的观点,如果你需要这样做,那么你可能应该改变你的数据库设计。
You can filter out rows that contain a NULL value in a specific column:
If you want to filter out rows that contain a null in any column then try this:
Update: Based on your comments, perhaps you want this?
And I agre with Martin that if you need to do this then you should probably change your database design.
这种方法的唯一缺点是只能比较 5 列,之后结果将始终为 false,因此我只比较可以为 NULL 的字段。
The only disadvantage of this approach is that you can only compare 5 columns, after that the result will always be false, so I do compare only the fields that can be
NULL
.我找到了这个解决方案:
此查询为每列选择最后一个非空值。
示例
如果您有一个表:
您会得到:
查询
我希望对您有所帮助。
I found this solution:
This query select last not null value for each column.
Example
If you have a table:
you get:
Query
I hope help you.
以下查询对我有用,
当我设置了列“NULL”的默认值时,
当我设置了默认值时,什么也没有
Following query working for me
when i have set default value of column 'NULL' then
and when i have set default value nothing then
我在 MySQL 中使用
\!
命令从 shell 中 grep 出 NULL 值:它与指定数据库/用户名/密码的正确
.my.cnf
配合使用效果最佳。这样你只需要用\ 包围你的
和select
即可! mysql e| grep -v NULL
。I use the
\!
command within MySQL to grep out NULL values from the shell:It works best with a proper
.my.cnf
where your database/username/password are specified. That way you just have to surround yourselect
with\! mysql e
and| grep -v NULL
.是的,在查询中使用
NOT NULL
,如下所示。Yes use
NOT NULL
in your query like this below.您甚至不必使用星号来获取表字段。
You didn't even have to use an asterisk to get the table fields.
为 int 列添加条件 >0,为 varchar 列添加 != ""
Add condition >0 for int columns and != "" for varchar columns
WHERE COALESCE(您的所有列)不为空
WHERE COALESCE(ALL YOUR COLUMNS) IS NOT NULL