Home Database Mysql Tutorial 讲解MySQL中<=>操作符的用法_MySQL

讲解MySQL中<=>操作符的用法_MySQL

Jun 01, 2016 pm 01:00 PM
mysql

问题 :

我在看以前的一个开发者的代码时看到
 

WHERE p.name <=> NULL
Copy after login

在这个查询语句中 <=>符号是什么意思啊?是不是和 =号是一样啊?还是一个语法错误啊?但是没有显示任何错误或者异常。我已经知道了mysql中的 <> = !=等符号。

最佳回答 :

和=号的相同点

像常规的=运算符一样,两个值进行比较,结果是0(不等于)或1(相等);换句话说:'A'<=>'B'得0和'a'<=>'a‘得1。

2.和=号的不同点

和=运算符不同的是,NULL的值是没有任何意义的。所以=号运算符不能把NULL作为有效的结果。所以:请使用<=>,

'a' <=> NULL 得0 NULL<=> NULL 得出 1。和=运算符正相反,=号运算符规则是 'a'=NULL 结果是NULL 甚至NULL = NULL 结果也是NULL。顺便说一句,mysql上几乎所有的操作符和函数都是这样工作的,因为和NULL比较基本上都没有意义。

用处

当两个操作数中可能含有NULL时,你需要一个一致的语句。

... WHERE col_a <=> &#63; ...

Copy after login

这里的占位符有可能是常量也有可能是NULL,当使用<=>运算符时,你没有必要对查询语句做任何修改。

相关操作符

除了 <=> ,还有两个其他的操作符用来处理某个值和NULL做比较,也就是IS NULL and IS NOT NULL。他们是ANSI标准中的一部分,因此也可以用在其他数据库中。而<=>只能在mysql中使用。

你可以把<=>当作mysql中的方言。

'a' IS NULL   ==> 'a' <=> NULL
'a' IS NOT NULL ==> NOT('a' <=> NULL)
Copy after login

据此,你可以把这个查询语句段改的更具移植性一点:

WHERE p.name IS NULL
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

What are the application scenarios of Java enumeration types in databases?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

Performance optimization strategies for PHP array paging Performance optimization strategies for PHP array paging May 02, 2024 am 09:27 AM

Performance optimization strategies for PHP array paging

See all articles