[英]select query in mysql always return empty result
I run this simple query with PHP and also phpMyAdmin: 我使用PHP以及phpMyAdmin运行此简单查询:
SELECT * FROM `rdm_order` WHERE `aff_result` != "xyc"
I checked. 我检查了。 table,column and rows are exist.
表,列和行都存在。
but the query return me empty result. 但查询返回空结果。
aff_result
is this format: VARCHAR(10) COLLATE utf8_persian_ci
aff_result
是这种格式: VARCHAR(10) COLLATE utf8_persian_ci
It seems all thing is correct. 看来所有事情都是正确的。 what is wrong?
怎么了?
Server: Localhost via UNIX socket
Server type: MySQL
Server version: 5.7.17-0ubuntu0.16.04.1-log - (Ubuntu)
Protocol version: 10
Server charset: UTF-8 Unicode (utf8)
Apache/2.4.25 (Unix) OpenSSL/1.0.2g
Database client version: libmysql - mysqlnd 5.0.12-dev
PHP extension: mysqliDocumentation curlDocumentation mbstringDocumentation
PHP version: 7.0.16
It's because the values are NULL
. 这是因为值是
NULL
。 NULL
cannot be captured by an =
or !=
comparison. 不能通过
=
或!=
比较捕获NULL
。 Use the following instead: 请改用以下内容:
SELECT *
FROM `rdm_order`
WHERE `aff_result` != 'xyc'
OR `aff_result` IS NULL
Use the NULL
-safe comparison operator: 使用
NULL
-safe比较操作:
SELECT *
FROM `rdm_order`
WHERE not `aff_result` <=> 'xyc';
The ANSI standard operator is is distinct from
. ANSI标准运算符
is distinct from
。
If you do not have any good reason to set default value of aff_result
as NULL, you can modify your aff_result
column and set default value as '' (blank string) using mysql alter command and your query will return the expected result set. 如果没有充分的理由将
aff_result
默认值设置为NULL,则可以使用mysql alter命令修改aff_result
列并将默认值设置为''(空白字符串),查询将返回预期的结果集。
As explained by @Siyual, NULL cannot be captured by an = or != comparison. 如@Siyual所述,不能通过=或!=比较捕获NULL。
And using OR operator in where clause is not index friendly. 并且在where子句中使用OR运算符对索引不友好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.