SQL =null Query results and is null to query the result description

Source: Internet
Author: User

When we want to query all result sets in SQL for which a column value is NULL, the query condition should write like this
SELECT * FROM table where field is null
Instead, use the SELECT * from table where field = NULL to not find the correct result. Then why is this? To understand why, you need to know the three-valued logic in SQL.
possible values for logical expressions in SQL include True, false, and unknown, which are called three-valued logic .

We know that in other languages, there are only two types of logical expressions, either True or false. In SQL, there is a third logical expression, unknown, which is unique in SQL. From the literal meaning we can solve the meaning of the deserve value is: What do not know, is nothing. In general, when we compare any value (including null itself) to NULL, we will return unknown. In query expressions, such as where and have, unknown is considered false. So we figured out why the select * from table where field =null couldn't find the correct result.

But not all unknown will be treated as false, and in a check constraint, unknown is treated as true. This is why we can also insert a null value into the field if we set the value of a field to be greater than or equal to 0, because the logical result of null>=0 in the check constraint is unknown treated as true.

It is important to note that in the grouping clause and the sort clause, SQL sees null as equal
That
1, groupby Will divide all null values into a group .
2,Order by will arrange all the null values together .

Go

SQL =null Query results and is null to query the result description

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.