在mysql中选择查询总是返回空结果

时间:2017-07-03 13:51:03

标签: php mysql sql

我使用PHP和phpMyAdmin运行这个简单的查询:

SELECT * FROM `rdm_order` WHERE `aff_result` != "xyc"
我检查了一下。表,列和行都存在。

但查询返回空结果。

aff_result的格式为:VARCHAR(10) COLLATE utf8_persian_ci

似乎一切都是正确的。怎么了?

Sample data

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

3 个答案:

答案 0 :(得分:2)

因为值为NULLNULL=比较无法捕获!=。请改用以下内容:

SELECT   * 
FROM     `rdm_order` 
WHERE    `aff_result` != 'xyc'
OR       `aff_result` IS NULL

答案 1 :(得分:0)

使用NULL - 安全比较运算符:

SELECT *
FROM `rdm_order`
WHERE not `aff_result` <=> 'xyc';

ANSI标准运算符为is distinct from

答案 2 :(得分:-1)

如果没有任何合理的理由将aff_result的默认值设置为NULL,则可以使用mysql alter命令修改aff_result列并将默认值设置为''(空字符串)您的查询将返回预期的结果集 正如@Siyual所解释的那样,= =或!=比较无法捕获NULL 并且在where子句中使用OR运算符不是索引友好的。

相关问题