MySQL Null value query problem

Source: Internet
Author: User
Tags mysql manual

I encountered a problem when I was developing a project within the company:
Select student_quality_id from student_quality where Mark_status=0 and batch_stauts in (2,3)
The result encountered a student_quality_id that had been unable to find a qualifying condition, and later found no consideration of the null value, modified to select student_quality_id from Student_quality where (Mark_ Status=0 or mark_status is null) and batch_stauts in (2,3) are OK, here is the handling of NULL values that I extracted from the MySQL manual.
The concept of null values is a common cause of confusion for beginners in SQL, and they often consider NULL to be the same thing as an empty string '. That's not true! For example, the following statements are completely different:
Mysql> INSERT into my_table (phone) VALUES (NULL);
Mysql> INSERT into my_table (phone) VALUES ("");
Two statements insert a value into the phone column, but the first one inserts a null value and the second inserts an empty string. The first meaning can be thought of as "phone number does not know", while the second can mean "she does not have a phone".
In SQL, the null value is always False (false) when any other value or even a null value is compared. An expression that contains null always produces a null value unless indicated in the documentation for the operators and functions contained in the expression. In the following example, all columns return null:
mysql> SELECT null,1+null,concat (' Invisible ', NULL);
If you want to look for a column with a value of NULL, you cannot use the =null test. The following statement does not return any rows because expr = null is false for any expression:
Mysql> SELECT * from my_table WHERE phone = NULL;
To find a null value, you must use the IS NULL test. The following example shows how to find a null phone number and an empty phone number:
Mysql> SELECT * FROM my_table WHERE phone is NULL;
Mysql> SELECT * from my_table WHERE phone = "";
In MySQL, like many other SQL servers, you cannot index columns that can have null values. You must declare that this column is not NULL, and that you cannot insert null into the indexed columns.
When reading data with load data infile, an empty column is updated with ' '. If you want to have null values in a column, you should use N in the text file. The literal word ' NULL ' can also be used in certain situations. See 7.16 LOAD DATA infile syntax.
When an order by is used, a null value is first rendered. If you sort in descending order with DESC, the null value is displayed last. When group by is used, all null values are considered equal.
To help with null processing, you can use the is null and is not NULL operator and the ifnull () function.
For some column types, the null value is handled specially. If you insert null into the first timestamp column of a table, the current date and time are inserted. If you insert null into a auto_increment column, the next number in the order is inserted.

MySQL Null value query problem

Related Article

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.