当前位置:网站首页>Tutorial on principles and applications of database system (041) -- MySQL query (III): setting query conditions

Tutorial on principles and applications of database system (041) -- MySQL query (III): setting query conditions

2022-07-24 00:49:00 Rsda DBA_ WGX

Database system principle and Application Tutorial (041)—— MySQL Inquire about ( 3、 ... and ): Set query conditions

Can be used in query WHERE Keyword to specify query criteria , Query the data that meets a certain condition from the data table .

Be careful : because WHERE Keywords precede SELECT Keyword execution , Therefore, keywords cannot be used in conditions SELECT Subsequent alias , But you can use the alias of the table .

One 、 Operator

You can use comparison operators and logical operators to construct query conditions .

1、 Comparison operator

The comparison operator can compare the values of two data , Common comparison operators are as follows :

Comparison operator describe
= be equal to
<> or != It's not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
BETWEEN … AND In a certain range
IN In a collection
IS NULL Null value
LIKE Use wildcards to retrieve

2、 Logical operators

Logical operators include NOT(!)、AND and OR, It is to calculate multiple query conditions , Used to construct complex query conditions . Usage is as follows :

Logical operators describe
NOT Conditions 1 Yes, conditions 1 Inverse , If the condition 1 It's true , The result is false ; If the condition 1 For false , The result is true .
Conditions 1 AND Conditions 2 If the condition 1 And conditions 2 It's all true , The result is true .
Conditions 1 OR Conditions 2 Conditions 1 And conditions 2 One is true , The result is true .

Two 、 Examples of comparative operator usage

Use equal to (=)、 Greater than (>)、 Less than (<)、 Greater than or equal to (>=)、 Less than or equal to (<=)、 It's not equal to (!= or <>) Structural conditions .

1、 be equal to (=)

for example : The inquiry address is 【 Xinxiang City 】 Student information .

mysql> select * from student where addr=' Xinxiang City ';
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2012 |  Liu Xiaoqing     | 1999-10-11 00:00:00 | 13603732255 |  Xinxiang City     |
| S2023 |  trump     | 1999-06-21 00:00:00 | 13343735588 |  Xinxiang City     |
+-------+-----------+---------------------+-------------+-----------+
2 rows in set (0.00 sec)

2、 Greater than (>)

for example : The query result exceeds 90 The student number of the student , Name, course name and grades of the courses learned ..

/*
select s.s_id, s_name, c_name, score
from student s join score sc
on s.s_id = sc.s_id
join course c
on sc.c_id = c.c_id
where score > 90;
*/
mysql> select s.s_id, s_name, c_name, score
    -> from student s join score sc
    -> on s.s_id = sc.s_id
    -> join course c
    -> on sc.c_id = c.c_id
    -> where score > 90;
+-------+-----------+--------------+-------+
| s_id  | s_name    | c_name       | score |
+-------+-----------+--------------+-------+
| S2013 |  Cao mengde     |  Advanced mathematics      |    97 |
| S2014 |  Liu Yan       |  Infectious diseases      |    96 |
| S2022 |  Zhou Huajian     |  linear algebra      |    93 |
| S2023 |  trump     |  economic law        |    91 |
| S2024 |  Obama     |  linear algebra      |    97 |
+-------+-----------+--------------+-------+
5 rows in set (0.01 sec)

3、 Less than (<)

for example : Check the student number of the student who failed the exam , Name, course name and grades of the courses learned .

/*
select s.s_id, s_name, c_name, score
from student s join score sc
on s.s_id = sc.s_id
join course c
on sc.c_id = c.c_id
where score < 60;
*/
mysql> select s.s_id, s_name, c_name, score
    -> from student s join score sc
    -> on s.s_id = sc.s_id
    -> join course c
    -> on sc.c_id = c.c_id
    -> where score < 60;
+-------+-----------+-----------------+-------+
| s_id  | s_name    | c_name          | score |
+-------+-----------+-----------------+-------+
| S2012 |  Liu Xiaoqing     |  Advanced mathematics         |    52 |
| S2012 |  Liu Xiaoqing     |  linear algebra         |    55 |
| S2016 |  Liu Ruofei     |  pedagogy           |    56 |
| S2021 |  Dong Wenhua     |  Infectious diseases         |    57 |
| S2022 |  Zhou Huajian     |  criminal procedure law       |    47 |
| S2023 |  trump     |  criminal procedure law       |    56 |
| S2026 |  Zhang Xueyou     |  Advanced mathematics         |    59 |
| S2026 |  Zhang Xueyou     |  Infectious diseases         |    48 |
+-------+-----------+-----------------+-------+
8 rows in set (0.00 sec)

4、 Greater than or equal to (>=)

for example : Query is older than 24 year ( contain 24 year ) Student information

mysql> select * from student where year(now())-year(birth) >= 24;
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2013 |  Cao mengde     | 1998-02-13 00:00:00 | 13853735522 |  Zhengzhou city     |
| S2014 |  Liu Yan       | 1998-06-24 00:00:00 | 13623735335 |  Zhengzhou city     |
| S2026 |  Zhang Xueyou     | 1998-07-06 00:00:00 | 13743735566 |  Zhengzhou city     |
+-------+-----------+---------------------+-------------+-----------+
3 rows in set (0.00 sec)

5、 Less than or equal to (<=)

for example : Query age is less than 24 year ( contain 24 year ) Student information

mysql> select * from student where year(now())-year(birth) <= 24;
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2011 |  Zhang Xiaogang     | 1999-12-03 00:00:00 | 13163735775 |  Xinyang City     |
| S2012 |  Liu Xiaoqing     | 1999-10-11 00:00:00 | 13603732255 |  Xinxiang City     |
| S2013 |  Cao mengde     | 1998-02-13 00:00:00 | 13853735522 |  Zhengzhou city     |
| S2014 |  Liu Yan       | 1998-06-24 00:00:00 | 13623735335 |  Zhengzhou city     |
| S2015 |  Liu Yan       | 1999-07-06 00:00:00 | 13813735225 |  Xinyang City     |
| S2016 |  Liu Ruofei     | 2000-08-31 00:00:00 | 13683735533 |  Kaifeng City     |
| S2021 |  Dong Wenhua     | 2000-07-30 00:00:00 | 13533735564 |  Kaifeng City     |
| S2022 |  Zhou Huajian     | 1999-05-25 00:00:00 | 13243735578 |  Zhengzhou city     |
| S2023 |  trump     | 1999-06-21 00:00:00 | 13343735588 |  Xinxiang City     |
| S2024 |  Obama     | 2000-10-17 00:00:00 | 13843735885 |  Xinyang City     |
| S2025 |  Zhou Jianhua     | 2000-08-22 00:00:00 | 13788736655 |  Kaifeng City     |
| S2026 |  Zhang Xueyou     | 1998-07-06 00:00:00 | 13743735566 |  Zhengzhou city     |
| S2031 |  Lee Myung Bak     | 1999-10-26 00:00:00 | 13643732222 |  Zhengzhou city     |
| S2032 |  Vinci     | 1999-12-31 00:00:00 | 13043731234 |  Zhengzhou city     |
+-------+-----------+---------------------+-------------+-----------+
14 rows in set (0.00 sec)

6、 It's not equal to (!= or <>)

for example : The inquiry address is not in 【 Zhengzhou city 】 Student information .

mysql> select * from student where addr <> ' Zhengzhou city ';
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2011 |  Zhang Xiaogang     | 1999-12-03 00:00:00 | 13163735775 |  Xinyang City     |
| S2012 |  Liu Xiaoqing     | 1999-10-11 00:00:00 | 13603732255 |  Xinxiang City     |
| S2015 |  Liu Yan       | 1999-07-06 00:00:00 | 13813735225 |  Xinyang City     |
| S2016 |  Liu Ruofei     | 2000-08-31 00:00:00 | 13683735533 |  Kaifeng City     |
| S2021 |  Dong Wenhua     | 2000-07-30 00:00:00 | 13533735564 |  Kaifeng City     |
| S2023 |  trump     | 1999-06-21 00:00:00 | 13343735588 |  Xinxiang City     |
| S2024 |  Obama     | 2000-10-17 00:00:00 | 13843735885 |  Xinyang City     |
| S2025 |  Zhou Jianhua     | 2000-08-22 00:00:00 | 13788736655 |  Kaifeng City     |
+-------+-----------+---------------------+-------------+-----------+
8 rows in set (0.00 sec)

3、 ... and 、 Examples of logical operator usage

1、NOT(!)

NOT It means to negate a condition , You can use an exclamation point instead NOT.

for example : The inquiry address is not in 【 Zhengzhou city 】 Student information .

mysql> select * from student where not addr = ' Zhengzhou city ';
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2011 |  Zhang Xiaogang     | 1999-12-03 00:00:00 | 13163735775 |  Xinyang City     |
| S2012 |  Liu Xiaoqing     | 1999-10-11 00:00:00 | 13603732255 |  Xinxiang City     |
| S2015 |  Liu Yan       | 1999-07-06 00:00:00 | 13813735225 |  Xinyang City     |
| S2016 |  Liu Ruofei     | 2000-08-31 00:00:00 | 13683735533 |  Kaifeng City     |
| S2021 |  Dong Wenhua     | 2000-07-30 00:00:00 | 13533735564 |  Kaifeng City     |
| S2023 |  trump     | 1999-06-21 00:00:00 | 13343735588 |  Xinxiang City     |
| S2024 |  Obama     | 2000-10-17 00:00:00 | 13843735885 |  Xinyang City     |
| S2025 |  Zhou Jianhua     | 2000-08-22 00:00:00 | 13788736655 |  Kaifeng City     |
+-------+-----------+---------------------+-------------+-----------+
8 rows in set (0.00 sec)

2、AND

AND Indicates that the result is true when two conditions are true at the same time .

for example : The inquiry address is 【 Zhengzhou city 】 And last name 【 Liu 】 Of the students .

mysql> select * from student where addr=' Zhengzhou city ' and s_name like ' Liu %';
+-------+--------+---------------------+-------------+-----------+
| s_id  | s_name | birth               | phone       | addr      |
+-------+--------+---------------------+-------------+-----------+
| S2014 |  Liu Yan    | 1998-06-24 00:00:00 | 13623735335 |  Zhengzhou city     |
+-------+--------+---------------------+-------------+-----------+
1 row in set (0.00 sec)

3、OR

OR It means that as long as one of the two conditions holds, the result is true .

for example : The inquiry address is 【 Zhengzhou city 】 Or last name 【 Liu 】 Student information .

mysql> select * from student where addr=' Zhengzhou city ' or s_name like ' Liu %';
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2012 |  Liu Xiaoqing     | 1999-10-11 00:00:00 | 13603732255 |  Xinxiang City     |
| S2013 |  Cao mengde     | 1998-02-13 00:00:00 | 13853735522 |  Zhengzhou city     |
| S2014 |  Liu Yan       | 1998-06-24 00:00:00 | 13623735335 |  Zhengzhou city     |
| S2015 |  Liu Yan       | 1999-07-06 00:00:00 | 13813735225 |  Xinyang City     |
| S2016 |  Liu Ruofei     | 2000-08-31 00:00:00 | 13683735533 |  Kaifeng City     |
| S2022 |  Zhou Huajian     | 1999-05-25 00:00:00 | 13243735578 |  Zhengzhou city     |
| S2026 |  Zhang Xueyou     | 1998-07-06 00:00:00 | 13743735566 |  Zhengzhou city     |
| S2031 |  Lee Myung Bak     | 1999-10-26 00:00:00 | 13643732222 |  Zhengzhou city     |
| S2032 |  Vinci     | 1999-12-31 00:00:00 | 13043731234 |  Zhengzhou city     |
+-------+-----------+---------------------+-------------+-----------+
9 rows in set (0.00 sec)

4、 Priority of the logical operator

When NOT、AND and OR When used at the same time , The priority order is NOT → AND → OR. Of course , If you can't remember the priority , Brackets can be used .

(1) The inquiry address is not in 【 Zhengzhou city 】 And last name 【 Liu 】 Student information .

mysql> select * from student
    -> where s_name like ' Liu %' and not addr = ' Zhengzhou city ';
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2012 |  Liu Xiaoqing     | 1999-10-11 00:00:00 | 13603732255 |  Xinxiang City     |
| S2015 |  Liu Yan       | 1999-07-06 00:00:00 | 13813735225 |  Xinyang City     |
| S2016 |  Liu Ruofei     | 2000-08-31 00:00:00 | 13683735533 |  Kaifeng City     |
+-------+-----------+---------------------+-------------+-----------+
3 rows in set (0.00 sec)

(2) Search address 【 Zhengzhou city 】 or 【 Xinyang City 】 And last name 【 Liu 】 Student information .

--  because  and  Is greater than  or, therefore  or  The conditions on both sides must be bracketed 
--  If the conditions of use :s_name like ' Liu %' and addr = ' Zhengzhou city ' or addr = ' Xinyang City '
--  Indicates that the inquiry address is 【 Zhengzhou city 】 And last name 【 Liu 】 Students and their addresses are 【 Xinyang City 】 Of the students , among 【 Xinyang City 】 The student's surname is not necessarily 【 Liu 】.
mysql> select * from student where s_name like ' Liu %' and (addr = ' Zhengzhou city ' or addr = ' Xinyang City ');
+-------+--------+---------------------+-------------+-----------+
| s_id  | s_name | birth               | phone       | addr      |
+-------+--------+---------------------+-------------+-----------+
| S2014 |  Liu Yan    | 1998-06-24 00:00:00 | 13623735335 |  Zhengzhou city     |
| S2015 |  Liu Yan    | 1999-07-06 00:00:00 | 13813735225 |  Xinyang City     |
+-------+--------+---------------------+-------------+-----------+
2 rows in set (0.01 sec)

--  Remove brackets 
mysql> select * from student where s_name like ' Liu %' and addr = ' Zhengzhou city ' or addr = ' Xinyang City ';
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2011 |  Zhang Xiaogang     | 1999-12-03 00:00:00 | 13163735775 |  Xinyang City     |
| S2014 |  Liu Yan       | 1998-06-24 00:00:00 | 13623735335 |  Zhengzhou city     |
| S2015 |  Liu Yan       | 1999-07-06 00:00:00 | 13813735225 |  Xinyang City     |
| S2024 |  Obama     | 2000-10-17 00:00:00 | 13843735885 |  Xinyang City     |
+-------+-----------+---------------------+-------------+-----------+
4 rows in set (0.00 sec)

Four 、 Usage of several special operators

1、BETWEEN … AND

BETWEEN … AND It is used to limit the information searched in two values ( One represents the minimum , One represents the maximum , Including boundaries ) Between .

The syntax is as follows :

where  Name  BETWEEN m AND n
--  Equivalent to   Name  >= m and  Name  <= n
--  Find records that are not within a certain range , The format is as follows :
where  Name  NOT BETWEEN m AND n
--  or 
where NOT  Name  BETWEEN m AND n

for example : Inquire about 2000-1-1( contain 2000-1-1) To 2000-12-31( contain 2000-12-31) Information about students born between .

mysql> select * from student where birth between '2000-1-1' and '2000-12-31';
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2016 |  Liu Ruofei     | 2000-08-31 00:00:00 | 13683735533 |  Kaifeng City     |
| S2021 |  Dong Wenhua     | 2000-07-30 00:00:00 | 13533735564 |  Kaifeng City     |
| S2024 |  Obama     | 2000-10-17 00:00:00 | 13843735885 |  Xinyang City     |
| S2025 |  Zhou Jianhua     | 2000-08-22 00:00:00 | 13788736655 |  Kaifeng City     |
+-------+-----------+---------------------+-------------+-----------+
4 rows in set (0.00 sec)

--  The above condition is equivalent to :birth >= '2000-1-1' and birth <='2000-12-31'
mysql> select * from student where birth >= '2000-1-1' and birth <='2000-12-31';
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2016 |  Liu Ruofei     | 2000-08-31 00:00:00 | 13683735533 |  Kaifeng City     |
| S2021 |  Dong Wenhua     | 2000-07-30 00:00:00 | 13533735564 |  Kaifeng City     |
| S2024 |  Obama     | 2000-10-17 00:00:00 | 13843735885 |  Xinyang City     |
| S2025 |  Zhou Jianhua     | 2000-08-22 00:00:00 | 13788736655 |  Kaifeng City     |
+-------+-----------+---------------------+-------------+-----------+
4 rows in set (0.00 sec)

Inquire about 1999-1-1( Not included 1999-1-1) To 2000-12-31( Not included 2000-12-31) Information about students born between .

mysql> select * from student where birth not between '1999-1-1' and '2000-12-31';
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2013 |  Cao mengde     | 1998-02-13 00:00:00 | 13853735522 |  Zhengzhou city     |
| S2014 |  Liu Yan       | 1998-06-24 00:00:00 | 13623735335 |  Zhengzhou city     |
| S2026 |  Zhang Xueyou     | 1998-07-06 00:00:00 | 13743735566 |  Zhengzhou city     |
+-------+-----------+---------------------+-------------+-----------+
3 rows in set (0.00 sec)

mysql> select * from student where not birth between '1999-1-1' and '2000-12-31';
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2013 |  Cao mengde     | 1998-02-13 00:00:00 | 13853735522 |  Zhengzhou city     |
| S2014 |  Liu Yan       | 1998-06-24 00:00:00 | 13623735335 |  Zhengzhou city     |
| S2026 |  Zhang Xueyou     | 1998-07-06 00:00:00 | 13743735566 |  Zhengzhou city     |
+-------+-----------+---------------------+-------------+-----------+
3 rows in set (0.00 sec)

2、IN

Use IN The operator , Indicates that the data to be queried is in a set ,IN Operators can partially replace logical expressions OR.

The syntax is as follows :

WHERE  Name  IN ( data 1,  data 2, ...)
--  or 
WHERE  Name  IN (select  Subquery )

for example :

(1) The inquiry address is 【 Xinxiang City 】 and 【 Xinyang City 】 Student information .

--  Use logical operators  or
mysql> select * from student where addr = ' Xinxiang City ' or addr = ' Xinyang City ';
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2011 |  Zhang Xiaogang     | 1999-12-03 00:00:00 | 13163735775 |  Xinyang City     |
| S2012 |  Liu Xiaoqing     | 1999-10-11 00:00:00 | 13603732255 |  Xinxiang City     |
| S2015 |  Liu Yan       | 1999-07-06 00:00:00 | 13813735225 |  Xinyang City     |
| S2023 |  trump     | 1999-06-21 00:00:00 | 13343735588 |  Xinxiang City     |
| S2024 |  Obama     | 2000-10-17 00:00:00 | 13843735885 |  Xinyang City     |
+-------+-----------+---------------------+-------------+-----------+
5 rows in set (0.00 sec)

--  Using operators  IN
mysql> select * from student where addr IN (' Xinxiang City ', ' Xinyang City ');
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2011 |  Zhang Xiaogang     | 1999-12-03 00:00:00 | 13163735775 |  Xinyang City     |
| S2012 |  Liu Xiaoqing     | 1999-10-11 00:00:00 | 13603732255 |  Xinxiang City     |
| S2015 |  Liu Yan       | 1999-07-06 00:00:00 | 13813735225 |  Xinyang City     |
| S2023 |  trump     | 1999-06-21 00:00:00 | 13343735588 |  Xinxiang City     |
| S2024 |  Obama     | 2000-10-17 00:00:00 | 13843735885 |  Xinyang City     |
+-------+-----------+---------------------+-------------+-----------+
5 rows in set (0.00 sec)

(2) Query and last name 【 Zhou 】 Students with the same address .

mysql> select s_name, addr from student where s_name like ' Zhou %';
+-----------+-----------+
| s_name    | addr      |
+-----------+-----------+
|  Zhou Huajian     |  Zhengzhou city     |
|  Zhou Jianhua     |  Kaifeng City     |
+-----------+-----------+
2 rows in set (0.00 sec)

--  Use subquery , Only operators can be used at this time  IN, The query result is actually the address 【 Zhengzhou city 】 and 【 Kaifeng City 】 Student information .
mysql> select * from student where addr IN
    -> (select addr from student where s_name like ' Zhou %');
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2013 |  Cao mengde     | 1998-02-13 00:00:00 | 13853735522 |  Zhengzhou city     |
| S2014 |  Liu Yan       | 1998-06-24 00:00:00 | 13623735335 |  Zhengzhou city     |
| S2022 |  Zhou Huajian     | 1999-05-25 00:00:00 | 13243735578 |  Zhengzhou city     |
| S2026 |  Zhang Xueyou     | 1998-07-06 00:00:00 | 13743735566 |  Zhengzhou city     |
| S2031 |  Lee Myung Bak     | 1999-10-26 00:00:00 | 13643732222 |  Zhengzhou city     |
| S2032 |  Vinci     | 1999-12-31 00:00:00 | 13043731234 |  Zhengzhou city     |
| S2016 |  Liu Ruofei     | 2000-08-31 00:00:00 | 13683735533 |  Kaifeng City     |
| S2021 |  Dong Wenhua     | 2000-07-30 00:00:00 | 13533735564 |  Kaifeng City     |
| S2025 |  Zhou Jianhua     | 2000-08-22 00:00:00 | 13788736655 |  Kaifeng City     |
+-------+-----------+---------------------+-------------+-----------+
9 rows in set (0.00 sec)

3、IS NULL

Use IS NULL You can query a record whose column is empty . The equal sign cannot be used to query a record with a null field ( because NULL And any value = The result of the comparison is NULL), Need to use IS NULL.

The syntax is as follows :

--  Query a record with empty column 
where  Name  IS NULL

--  Query a record whose column is not empty 
where  Name  IS NOT NULL
--  or  
where NOT  Name  IS NULL

for example : There is the following table t1.

mysql> create table t1(id int primary key,name char(20));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t1 values(1,'Jack'),(2,null),(3,'Black'),(4,null),(5,null);
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from t1;
+----+-------+
| id | name  |
+----+-------+
|  1 | Jack  |
|  2 | NULL  |
|  3 | Black |
|  4 | NULL  |
|  5 | NULL  |
+----+-------+
5 rows in set (0.00 sec)

(1) Query name is NULL The record of .

mysql> select * from t1 where name is null;
+----+------+
| id | name |
+----+------+
|  2 | NULL |
|  4 | NULL |
|  5 | NULL |
+----+------+
3 rows in set (0.00 sec)

--  Use equal sign (=) No results 
mysql> select * from t1 where name = null;
Empty set (0.00 sec)

(2) The search name is not NULL The record of .

mysql> select * from t1 where name is not null;
+----+-------+
| id | name  |
+----+-------+
|  1 | Jack  |
|  3 | Black |
+----+-------+
2 rows in set (0.00 sec)

mysql> select * from t1 where not name is null;
+----+-------+
| id | name  |
+----+-------+
|  1 | Jack  |
|  3 | Black |
+----+-------+
2 rows in set (0.00 sec)
原网站

版权声明
本文为[Rsda DBA_ WGX]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230648567216.html