0

使用mysql“=null”的问题

 1 year ago
source link: https://blog.p2hp.com/archives/9659
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

使用mysql“=null”的问题 | Lenix Blog

select
	last_name,
	job_id
from employees
where manager_id is null;

select
	last_name,
	job_id
from employees
where manager_id <=> Null;

select
	last_name,
	job_id
from employees
where manager_id = Null;

最后一个 = null 是错误的
如果等号两边的值、字符串或表达式中有一个为NULL,则比较结果为NULL。
所以要用 <=>
安全等于运算符 安全等于运算符(<=>)与等于运算符(=)的作用是相似的, 唯一的区别 是‘<=>’可
以用来对NULL进行判断。在两个操作数均为NULL时,其返回值为1,而不为NULL;当一个操作数为NULL时,其返回值为0,而不为NULL。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK