使用 MySQL 检查空字段

Checking for an empty field with MySQL(使用 MySQL 检查空字段)
本文介绍了使用 MySQL 检查空字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个查询来检查符合特定条件的用户,其中一个是他们有电子邮件地址.

I've wrote a query to check for users with certain criteria, one being they have an email address.

我们的网站将允许用户拥有或不拥有电子邮件地址.

Our site will allow a user to have or not have an email address.

$aUsers=$this->readToArray('
 SELECT `userID` 
 FROM `users` 
 WHERE `userID` 
 IN(SELECT `userID`
         FROM `users_indvSettings`
  WHERE `indvSettingID`=5 AND `optionID`='.$time.')
  AND `email`!=""
 ');

这是在 SQL 中检查空字段的最佳方法吗?我刚刚尝试了IS NOT NULL",这仍然返回了一个用户记录,而他们没有电子邮件地址.

Is this the best way to check for an empty field in SQL? I've just tried "IS NOT NULL" and this still returned a users record without them having an email address.

上面的查询有效,但出于好奇,我想知道我是否以正确的方式进行操作.

The query above works but out of curiosity I wondered if I'm doing it the correct way.

推荐答案

空字段可以是空字符串或 NULL.

An empty field can be either an empty string or a NULL.

要同时处理这两种情况,请使用:

To handle both, use:

email > ''

如果您的表格中有大量空电子邮件记录(两种类型),则可以从 range 访问中受益.

which can benefit from the range access if you have lots of empty email record (both types) in your table.

这篇关于使用 MySQL 检查空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)