MySQL collapse 7: query filtering conditions

Weng Zhihua 2020-11-09 16:59:11
mysql collapse query filtering conditions


summary

In the actual business scenario application , We often need to get and filter our target data based on business conditions . This process is called data query filtering . And the various conditions used in the filtration process ( Like date time 、 user 、 state ) It's a necessary step for us to get accurate data ,

So that we can get the results we want . So in this chapter we'll learn about MySQL Various uses of query filter conditions in .

Relationship between operation

Relational operations are where Statement followed by the preceding or n Conditions , Satisfy where The data of the latter condition will be returned , Otherwise, the unsatisfied will be filtered out .operators It refers to the operator , There are several situations :

Operator explain
= be equal to
<> perhaps != It's not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to

 

The basic syntax of relational operations is as follows :

1 select cname1,cname2,... from tname where cname operators cval 

be equal to =

Query out Data whose columns and subsequent values are exactly equal , Non value types need to put quotation marks on the following values , Value type does not need .

The syntax is as follows :

1 select cname1,cname2,... from tname where cname = cval; 
 1 mysql> select * from user2;
 2 +----+-------+-----+----------+-----+
 3 | id | name | age | address | sex |
 4 +----+-------+-----+----------+-----+
 5 | 1 | brand | 21 | fuzhou | 1 |
 6 | 2 | helen | 20 | quanzhou | 0 |
 7 | 3 | sol | 21 | xiamen | 0 |
 8 +----+-------+-----+----------+-----+
 9 3 rows in set
10
11 mysql> select * from user2 where name='helen';
12 +----+-------+-----+----------+-----+
13 | id | name | age | address | sex |
14 +----+-------+-----+----------+-----+
15 | 2 | helen | 20 | quanzhou | 0 |
16 +----+-------+-----+----------+-----+
17 1 row in set
18
19 mysql> select * from user2 where age=21;
20 +----+-------+-----+---------+-----+
21 | id | name | age | address | sex |
22 +----+-------+-----+---------+-----+
23 | 1 | brand | 21 | fuzhou | 1 |
24 | 3 | sol | 21 | xiamen | 0 |
25 +----+-------+-----+---------+-----+
26 2 rows in set

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

There are not two ways of writing , One is <>, The other is !=, It means the same thing , You can switch to use at will , however <> Precede != appear , So look at a lot of previous examples ,<> The frequency of occurrence is relatively high , More portability , Recommended .

The purpose of not equal is to find out the result that does not match the condition , The format is as follows :

select cname1,cname2,... from tname where cname <> cval;
or
select cname1,cname2,... from tname where cname != cval;

 

 1 mysql> select * from user2;
 2 +----+-------+-----+----------+-----+
 3 | id | name | age | address | sex |
 4 +----+-------+-----+----------+-----+
 5 | 1 | brand | 21 | fuzhou | 1 |
 6 | 2 | helen | 20 | quanzhou | 0 |
 7 | 3 | sol | 21 | xiamen | 0 |
 8 +----+-------+-----+----------+-----+
 9 3 rows in set
10
11 mysql> select * from user2 where age<>20;
12 +----+-------+-----+---------+-----+
13 | id | name | age | address | sex |
14 +----+-------+-----+---------+-----+
15 | 1 | brand | 21 | fuzhou | 1 |
16 | 3 | sol | 21 | xiamen | 0 |
17 +----+-------+-----+---------+-----+
18 2 rows in set 

Greater than less than (> <)

It's usually used for numbers or dates 、 Comparison of time types , The format is as follows :

1 select cname1,cname2,... from tname where cname > cval;
2
3 select cname1,cname2,... from tname where cname < cval;
4
5 select cname1,cname2,... from tname where cname >= cval;
6
7 select cname1,cname2,... from tname where cname <= cval;

 

 1 mysql> select * from user2 where age>20;
 2 +----+-------+-----+---------+-----+
 3 | id | name | age | address | sex |
 4 +----+-------+-----+---------+-----+
 5 | 1 | brand | 21 | fuzhou | 1 |
 6 | 3 | sol | 21 | xiamen | 0 |
 7 +----+-------+-----+---------+-----+
 8 2 rows in set
 9
10 mysql> select * from user2 where age>=20;
11 +----+-------+-----+----------+-----+
12 | id | name | age | address | sex |
13 +----+-------+-----+----------+-----+
14 | 1 | brand | 21 | fuzhou | 1 |
15 | 2 | helen | 20 | quanzhou | 0 |
16 | 3 | sol | 21 | xiamen | 0 |
17 +----+-------+-----+----------+-----+
18 3 rows in set
19
20 mysql> select * from user2 where age<21;
21 +----+-------+-----+----------+-----+
22 | id | name | age | address | sex |
23 +----+-------+-----+----------+-----+
24 | 2 | helen | 20 | quanzhou | 0 |
25 +----+-------+-----+----------+-----+
26 1 row in set
27
28 mysql> select * from user2 where age<=21;
29 +----+-------+-----+----------+-----+
30 | id | name | age | address | sex |
31 +----+-------+-----+----------+-----+
32 | 1 | brand | 21 | fuzhou | 1 |
33 | 2 | helen | 20 | quanzhou | 0 |
34 | 3 | sol | 21 | xiamen | 0 |
35 +----+-------+-----+----------+-----+
36 3 rows in set

Logical operations

 

Operator explain
AND Many conditions hold
OR Satisfy one of many conditions
NOT Take no action on the condition

AND( And )

When multiple conditions are needed to filter data , In this way ,and Every expression of is to hold , The filtered data is what users need .

Let's filter out the data that both age and gender hold , The syntax is as follows : 

1 select cname1,cname2,... from tname where cname1 operators cval1 and cname2 operators cval2  
 1 mysql> select * from user2;
 2 +----+-------+-----+----------+-----+
 3 | id | name | age | address | sex |
 4 +----+-------+-----+----------+-----+
 5 | 1 | brand | 21 | fuzhou | 1 |
 6 | 2 | helen | 20 | quanzhou | 0 |
 7 | 3 | sol | 21 | xiamen | 0 |
 8 | 4 | weng | 33 | guizhou | 1 |
 9 +----+-------+-----+----------+-----+
10 4 rows in set
11
12 mysql> select * from user2 where age >20 and sex=1;
13 +----+-------+-----+---------+-----+
14 | id | name | age | address | sex |
15 +----+-------+-----+---------+-----+
16 | 1 | brand | 21 | fuzhou | 1 |
17 | 4 | weng | 33 | guizhou | 1 |
18 +----+-------+-----+---------+-----+
19 2 rows in set 

OR( or )

When one of the conditions is satisfied, the data is filtered .

The following conditions filter out that the age is older than 21 Years old and less than 21 Year old data , The syntax is as follows :

1 select cname1,cname2,... from tname where cname1 operators cval1 or cname2 operators cval2 

 

 1 mysql> select * from user2;
 2 +----+-------+-----+----------+-----+
 3 | id | name | age | address | sex |
 4 +----+-------+-----+----------+-----+
 5 | 1 | brand | 21 | fuzhou | 1 |
 6 | 2 | helen | 20 | quanzhou | 0 |
 7 | 3 | sol | 21 | xiamen | 0 |
 8 | 4 | weng | 33 | guizhou | 1 |
 9 +----+-------+-----+----------+-----+
10 4 rows in set
11
12 mysql> select * from user2 where age>21 or age<21;
13 +----+-------+-----+----------+-----+
14 | id | name | age | address | sex |
15 +----+-------+-----+----------+-----+
16 | 2 | helen | 20 | quanzhou | 0 |
17 | 4 | weng | 33 | guizhou | 1 |
18 +----+-------+-----+----------+-----+
19 2 rows in set 

NOT( Take the )

Negate a condition that is satisfied , The filtered data is what users need . 

The following filter does not belong to the age older than 20 The data of , The syntax is as follows :

1 select cname1,cname2,... from tname where not(cname operators cval) 
 1 mysql> select * from user2;
 2 +----+-------+-----+----------+-----+
 3 | id | name | age | address | sex |
 4 +----+-------+-----+----------+-----+
 5 | 1 | brand | 21 | fuzhou | 1 |
 6 | 2 | helen | 20 | quanzhou | 0 |
 7 | 3 | sol | 21 | xiamen | 0 |
 8 | 4 | weng | 33 | guizhou | 1 |
 9 +----+-------+-----+----------+-----+
10 4 rows in set
11
12 mysql> select * from user2 where not(age>20);
13 +----+-------+-----+----------+-----+
14 | id | name | age | address | sex |
15 +----+-------+-----+----------+-----+
16 | 2 | helen | 20 | quanzhou | 0 |
17 +----+-------+-----+----------+-----+
18 1 row in set 

Fuzzy matching

It's like the user table information table above us ( Include name 、 Age 、 Address 、 Gender ), When we want to query the name s At the beginning of the user , You can use like Keyword. , He used it to blur the data .

The syntax is as follows ,pattern It can contain wildcards , There are two kinds of .%: Represents a match to any one or n Characters ; _: To match any character .

1 select cname1,cname2,... from tname where cname like pattern; 

% Use

 1 mysql> select * from user2;
 2 +----+--------+-----+----------+-----+
 3 | id | name | age | address | sex |
 4 +----+--------+-----+----------+-----+
 5 | 1 | brand | 21 | fuzhou | 1 |
 6 | 2 | helen | 20 | quanzhou | 0 |
 7 | 3 | sol | 21 | xiamen | 0 |
 8 | 4 | weng | 33 | guizhou | 1 |
 9 | 5 | selina | 25 | taiwang | 0 |
10 +----+--------+-----+----------+-----+
11 5 rows in set
12
13 mysql> select * from user2 where name like 's%';
14 +----+--------+-----+---------+-----+
15 | id | name | age | address | sex |
16 +----+--------+-----+---------+-----+
17 | 3 | sol | 21 | xiamen | 0 |
18 | 5 | selina | 25 | taiwang | 0 |
19 +----+--------+-----+---------+-----+
20 2 rows in set 

_ Use

 1 mysql> select * from user2;
 2 +----+--------+-----+----------+-----+
 3 | id | name | age | address | sex |
 4 +----+--------+-----+----------+-----+
 5 | 1 | brand | 21 | fuzhou | 1 |
 6 | 2 | helen | 20 | quanzhou | 0 |
 7 | 3 | sol | 21 | xiamen | 0 |
 8 | 4 | weng | 33 | guizhou | 1 |
 9 | 5 | selina | 25 | taiwang | 0 |
10 +----+--------+-----+----------+-----+
11 5 rows in set
12
13 mysql> select * from user2 where name like 's_l';
14 +----+------+-----+---------+-----+
15 | id | name | age | address | sex |
16 +----+------+-----+---------+-----+
17 | 3 | sol | 21 | xiamen | 0 |
18 +----+------+-----+---------+-----+
19 1 row in set 

Be careful

1、 Don't overuse fuzzy matching wildcards . If other operators do the same thing , Other operators should be used
2、
Try not to match a large number of tables with fuzzy matching % start , such as like '%username', This will scan the table , Less efficient . Try to be clear about the beginning of the fuzzy search , such as like 'brand%', It's going to be located first brand Initial data , It's a lot more efficient .

Check the range of values

BETWEEN AND( Interval query )

The operator BETWEEN … AND Data range between two values will be selected , These values can be numerical 、 Text or date , Belong to a closed interval query .

and Left side val1 and On the right val2 Two critical values respectively , Equivalent to a mathematical formula [val1,val2] , Data belonging to these two intervals will be filtered out (>=val1 and <=val2), So the syntax is as follows :

1 selec cname1,cname2,... from tname where cname between val1 and val2;
2  Equate to
3 selec cname1,cname2,... from tname where cname >= val1 and cname <= val2;

The age of inquiry is [21,25] Data between :

 1 mysql> select * from user2;
 2 +----+--------+-----+----------+-----+
 3 | id | name | age | address | sex |
 4 +----+--------+-----+----------+-----+
 5 | 1 | brand | 21 | fuzhou | 1 |
 6 | 2 | helen | 20 | quanzhou | 0 |
 7 | 3 | sol | 21 | xiamen | 0 |
 8 | 4 | weng | 33 | guizhou | 1 |
 9 | 5 | selina | 25 | taiwang | 0 |
10 +----+--------+-----+----------+-----+
11 5 rows in set
12
13 mysql> select * from user2 where age between 21 and 25;
14 +----+--------+-----+---------+-----+
15 | id | name | age | address | sex |
16 +----+--------+-----+---------+-----+
17 | 1 | brand | 21 | fuzhou | 1 |
18 | 3 | sol | 21 | xiamen | 0 |
19 | 5 | selina | 25 | taiwang | 0 |
20 +----+--------+-----+---------+-----+
21 3 rows in set
22
23 mysql> select * from user2 where age >= 21 and age <= 25;
24 +----+--------+-----+---------+-----+
25 | id | name | age | address | sex |
26 +----+--------+-----+---------+-----+
27 | 1 | brand | 21 | fuzhou | 1 |
28 | 3 | sol | 21 | xiamen | 0 |
29 | 5 | selina | 25 | taiwang | 0 |
30 +----+--------+-----+---------+-----+
31 3 rows in set

IN( Contains the query )

According to the data above , If we want to find out the user data of Fuzhou and Xiamen , You should use IN The operator , because IN The operator allows us to WHERE Clause to specify multiple values , One of these values corresponds to , Return data when the condition is satisfied .

The syntax is as follows ,in The value types in the following list must be consistent or compatible , And does not support wildcards :

1 select cname1,cname2,... from tname where cname in (val1,val2,...);

 

 1 mysql> select * from user2;
 2 +----+--------+-----+----------+-----+
 3 | id | name | age | address | sex |
 4 +----+--------+-----+----------+-----+
 5 | 1 | brand | 21 | fuzhou | 1 |
 6 | 2 | helen | 20 | quanzhou | 0 |
 7 | 3 | sol | 21 | xiamen | 0 |
 8 | 4 | weng | 33 | guizhou | 1 |
 9 | 5 | selina | 25 | taiwang | 0 |
10 +----+--------+-----+----------+-----+
11 5 rows in set
12
13 mysql> select * from user2 where address in('fuzhou','xiamen');
14 +----+-------+-----+---------+-----+
15 | id | name | age | address | sex |
16 +----+-------+-----+---------+-----+
17 | 1 | brand | 21 | fuzhou | 1 |
18 | 3 | sol | 21 | xiamen | 0 |
19 +----+-------+-----+---------+-----+
20 2 rows in set

NOT IN( Reverse the inclusion query )

We've learned from it not Get users , Yes not After the execution of the expression to reverse the operation , Under test :

 1 mysql> select * from user2;
 2 +----+--------+-----+----------+-----+
 3 | id | name | age | address | sex |
 4 +----+--------+-----+----------+-----+
 5 | 1 | brand | 21 | fuzhou | 1 |
 6 | 2 | helen | 20 | quanzhou | 0 |
 7 | 3 | sol | 21 | xiamen | 0 |
 8 | 4 | weng | 33 | guizhou | 1 |
 9 | 5 | selina | 25 | taiwang | 0 |
10 +----+--------+-----+----------+-----+
11 5 rows in set
12
13 mysql> select * from user2 where address not in('fuzhou','quanzhou','xiamen');
14 +----+--------+-----+---------+-----+
15 | id | name | age | address | sex |
16 +----+--------+-----+---------+-----+
17 | 4 | weng | 33 | guizhou | 1 |
18 | 5 | selina | 25 | taiwang | 0 |
19 +----+--------+-----+---------+-----+
20 2 rows in set

Null check

IS NULL/IS NOT NULL

Determine whether it is null , The syntax is as follows , Here's the thing to notice , The right value is null The data of , Various comparisons Operator 、likebetween andinnot in The query doesn't work , Only is null It can filter out .

1 select cname1,cname2,... from tname where cname is null;
2  perhaps
3 select cname1,cname2,... from tname where cname is not null;

 

 1 mysql> select * from user2 where address is null;
 2 +----+--------+-----+---------+-----+
 3 | id | name | age | address | sex |
 4 +----+--------+-----+---------+-----+
 5 | 5 | selina | 25 | NULL | 0 |
 6 +----+--------+-----+---------+-----+
 7 1 row in set
 8
 9 mysql> select * from user2 where address is not null;
10 +----+-------+-----+----------+-----+
11 | id | name | age | address | sex |
12 +----+-------+-----+----------+-----+
13 | 1 | brand | 21 | fuzhou | 1 |
14 | 2 | helen | 20 | quanzhou | 0 |
15 | 3 | sol | 21 | xiamen | 0 |
16 | 4 | weng | 33 | guizhou | 1 |
17 +----+-------+-----+----------+-----+
18 4 rows in set

 

There is a keyword <=>, It can include the right to null It's worth judging , But now it's less used , If you are interested, you can check , I won't go over it here .

summary

1、like In the expression % Match one or more arbitrary characters ,_ Match an arbitrary character

2、 Null value query needs to use IS NULL perhaps IS NOT NULL, Other query operator pairs NULL Invalid value . Even if % Wildcards can match anything , It doesn't match the value NULL The data of

 

3、 It is recommended to create tables , The table field is not set to be empty , Give the field a default The default value is .

4、MySQL Support use NOT Yes IN BETWEEN and EXISTS Clause negates .

 

版权声明
本文为[Weng Zhihua]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云