How to use the <=> operator in MySQL _ MySQL

Source: Internet
Author: User
This article mainly introduces the usage of operators in MySQL and sorts out the actual problems related to stackoverflow. For more information, see Problem:

When I read the code of a previous developer

WHERE p.name <=> NULL

In this query statement, what does the <=> symbol mean? Is it the same as the =? Or a syntax error? However, no errors or exceptions are displayed. I already know <> =! =.

Best answer:

Similarities with =

Like the regular = operator, two values are compared and the result is 0 (not equal to) or 1 (equal); in other words: 'A' <=> 'B' gets 0 and 'A' <=> 'A' gets 1.

2. differences between and =

Unlike the = operator, NULL does not make any sense. Therefore, the = operator cannot use NULL as a valid result. Therefore, use <=>,

'A' <=> NULL: 0 NULL <=> NULL: 1. In contrast to the = operator, the = operator rule is 'a' = NULL, and the result is NULL. By the way, almost all operators and functions in mysql work like this, because it is meaningless to compare with NULL.

Usage

When two operands may contain NULL, you need a consistent statement.

... WHERE col_a <=> ? ...

Here, placeholders may be constants or NULL. when you use the <=> operator, you do not need to modify the query statement.

Related operators

In addition to <=>, there are two other operators used to compare a value with NULL, that IS, is null and is not null. They are part of the ANSI standard and therefore can be used in other databases. And <=> can only be used in mysql.

You can use <=> as a dialect in mysql.

'a' IS NULL   ==> 'a' <=> NULL'a' IS NOT NULL ==> NOT('a' <=> NULL)

Accordingly, you can modify the query statement segment to make it more portable:

WHERE p.name IS NULL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.