mysql 选择等于空值,MySQL选择包含空值的字段

In our company we moved our web application (LAMP) from one server (Ubuntu 10.04) to a new server (Ubuntu 12.04.2). Now we encountered a strange behavior I haven't seen before and I really do not know where to begin. Maybe someone can give me hint.

We have got the following simple table:

id data1 data2 data3

(int) (varchar) (int) (int)

-------------------------------------

1 (empty) 123 456

2 (null) 321 654

3 abc 555 666

(empty) means the field contains a empty string. (null) means that the field is null. Now we use the following very very simple query:

SELECT * FROM `table` WHERE `data1` != 'abc';

On our old server the query returned the lines with the ids 1 and 2 which, I guess, is absolutely correct since !='abc' matches those two recordsets.

On our new server the query only returns the recordset with the id 1. Recordsets containing null in the select fields are suddenly ignored by the query somehow.

Just to make it more clear: I know that IS NULL could be used, but that would result in checking all queries and tables in the application matching this situation.

Now the questions are:

Did we had luck on our old server that the query behaved as expected by returning lines 1 and 2 or does the new server behave correct by returning only line 1?

Generally: Should !='abc' match the recordsets 1 and 2 or should it only match id 1?

Is it possible that there is a setting in the mysql configuration that controlls that behaviour? I am a little stuck with that. Every help is appreciated!

Thanks in advance...

解决方案

Because null is a special case, if you want null values to be included, you should explicitly specify that you want them.

SELECT * FROM table WHERE (data1 <> 'abc' or data1 is null)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值