当前位置:网站首页>Common SQL statement query
Common SQL statement query
2022-07-28 02:30:00 【51CTO】
The overall syntax of the query :select Field list from Library name . Table name select Field list from Library name . Table name where Conditional expression
notes :group by grouping having Filtering groups order by Sort asc Ascending desc Descending
1、 Query age is greater than 21 Year old employees :
2、 Inquiry age is not equal to 21 Year old employees :
Query the records that meet the conditions ,in ()
3、 The age of inquiry is 18 Years old and 21 Year old :
Query records that do not meet the conditions ,not in ()
4、 With field (f_name) Sort :
5、 Query all s_id It's not equal to 101 Also is not equal to 102 The record of :
belt BETWEEN AND The scope of inquiry
6、and In what range :
SELECT * from Table name where Field between 18 and 23;
7、 The query in A record And A record outside The record of :
SELECT * from Table name where Field not between 18 and 23;
Fuzzy query
8、 Inquire about f_name It contains letters 'g' The record of :
SELECT * from Table name where f_name like '%g%';
9、 Query to 'b' start , And 'y' The name of the fruit at the end :
SELECT * from Table name where f_name like 'b%y';
10、 Query with letters 'y' ending , And 'y' There is only 4 A letter record :
SELECT * from Table name where f_name like '____y';
Query is an empty record
11、 Query the fields in the table (c_email) Empty record :
SELECT * from Table name where c_email is null;
The query is not an empty record
12、 Query the fields in the table (c_email) Not empty record :
SELECT * from Table name where c_email is not null;
belt AND Multi criteria query of
13、 Inquire about s_id = 101, also f_price Greater than or equal to 5 Of :
SELECT * from Table name where s_id = '101' and f_price >=5;
14、 Inquire about s_id = 101 perhaps 102, And f_price Greater than 5, also f_name='apple' Of :
SELECT * from Table name where s_id in('101', '102') and f_price >= 5 and f_name = 'apple';
belt OR Multi criteria query of
15、 Inquire about s_id=101 perhaps s_id=102 Of :
SELECT * from Table name where s_id = 101 OR s_id = 102;
Go to check again
16、 Inquire about fruits In the table s_id Value of field , return s_id Field values must not be repeated :
SELECT distinct s_id FROM fruits;
Ascending and descending
order by Field name Ascending order by Field name desc Descending
17、 Query table f_name field value , And sort it :
SELECT f_name FROM Table name order by f_name;
18、 Look up f_name and f_price Field , According to the first f_name Sort , Press again f_price Sort :
SELECT * from Table name order by f_name, f_price;
19、 Look up f_name and f_price Field , Press for results f_price Descending Sort by :
SELECT * from from order by f_price desc;
20、 Query table , According to the first f_price null , Press again f_name Sort fields in ascending order :
SELECT * from Table name order by f_price desc, f_name;
Group query
21、 Arrange in order and remove the duplicates :
select * from Table name group by s_id;
22、 according to s_id Group the data in the table , It also shows that the type of fruit is greater than 1 Group information :
SELECT s_id, GROUP_CONCAT(f_name) AS Names FROM fruits GROUP BY s_id HAVING COUNT(f_name) > 1;
23、 stay group by Used in clauses with rollup:
SELECT * from Table name group by Field with rollup;
Multi field grouping
24、 according to s_id and f_name Field groups the data in the table :
SELECT * FROM Table name group by s_id,f_name;
GROUP BY and ORDER BY Use it together
25、SELECT * from Table name group by u_name having u_age > 20 order by u_id;
use LIMIT Limit the number of query results
26、 Count the total number of lines :
SELECT count(*) From Table name ;
26、 Show fruits Table top of query results 4 That's ok :
SELECT * From Table name limit 4;
27、 From 5 It started with a record , The number of lines is... In length 3 The record of :
SELECT * From Table name limit 4, 3;
Use the set function to query
COUNT() Counting function SUM() Sum function AVG() The average function MAX() Maximum function MIN() Minimum function
28、 The total number of rows in the query table :
SELECT COUNT(*) AS cust_num FROM Table name ;
29、 The total number of customers with e-mail in the query table :
SELECT COUNT(c_email) AS email_num FROM Table name ;
30、 Query in the table 30005 The total amount of fruit purchased under Order No :
SELECT SUM(quantity) AS items_total FROM Table name WHERE o_num = 30005;
31、 In the table , Use SUM() Function to count the total amount of fruit ordered in different order numbers :
SELECT o_num, SUM(quantity) AS items_total FROM Table name GROUP BY o_num;
32、 In the table , Inquire about s_id=103 The average value of the supplier's fruit price :
SELECT AVG(f_price) AS avg_price from Table name WHERE s_id = 103;
33、 In the table , Query the average fruit price of each supplier :
SELECT s_id,AVG(f_price) AS avg_price from Table name GROUP BY s_id;
34、 Find the most expensive fruit on the market in the table :
SELECT MAX(f_price) AS max_price from Table name ;
35、 Find the most expensive fruits from different suppliers in the table :
SELECT s_id, MAX(f_price) AS max_price from Table name GROUP BY s_id;
36、 Find in table f_name The maximum of :
SELECT MAX(f_name) FROM fruits;
37、 Find the cheapest fruit on the market in the table :
SELECT MIN(f_price) AS min_price from Table name ;
38、 Find the lowest priced fruit from different suppliers in the table :
SELECT s_id, MIN(f_price) AS min_price from Table name GROUP BY s_id;
Link query
1. Internal connection query 2. External connection query 3. Composite conditional join query
LEFT JOIN The left table is full of records , The right table meets the conditions
RIGHT JOIN The right table is full of records , The left table meets the conditions
14、 Internal connection query :
SELECT Field from surface 1 INNER JOIN surface 2 ON Matching condition ;
40、 External connection query ( Left connection ):
select Field from surface 1 left join surface 2 on Matching condition ;
41、 External connection query ( The right connection ):
select Field from surface 1 left outer join surface 2 on Matching condition ;
42、 Composite conditional join query :
SELECT Field FROM surface 1 INNER JOIN surface 2 ON surface 1. Field 1 = surface 2. Field 1 AND Field 1 = 10001;
Subquery
belt ANY、SOME Subqueries for keywords
belt ALL Subqueries for keywords
belt EXISTS Subqueries for keywords
belt IN Subqueries for keywords
Subqueries with comparison operators merge query results
43、 belt ANY、SOME Subqueries for keywords :
SELECT num1 FROM surface 1 WHERE num1 > ANY (SELECT num2 FROM surface 2 where num2);
44、 belt ALL Subqueries for keywords ( return tbl1 Ratio in table tbl2 surface num2 Column all values are large ):
SELECT num1 FROM tbl1 WHERE num1 > ALL (SELECT num2 FROM tbl2);
45、 belt exists Subqueries for keywords ( Inquire about suppliers Is there... In the table s_id=107 Supplier , If there is , Then check fruits The records in the table ):
SELECT * FROM fruits WHERE exists (SELECT s_name FROM suppliers WHERE s_id = 107);
46、 Inquire about suppliers Is there... In the table s_id=107 Supplier , If it does not exist, query fruits The records in the table :
SELECT * FROM fruits WHERE NOT EXISTS(SELECT s_name FROM suppliers WHERE s_id = 107);
47、 belt IN Subqueries for keywords ( stay orderitems Query in table f_id by c0 Order number , And query the customer with the order number according to the order number c_id):
SELECT c_id FROM orders WHERE o_num IN(SELECT o_num FROM orderitems WHERE f_id = 'c0');
It can also be :
SELECT c_id FROM orders WHERE o_num IN (30003, 30005);
48、 A subquery with a comparison operator ( stay suppliers Query in table s_city be equal to “Tianjin” Supplier s_id, And then in fruits Query the types of fruits provided by the supplier in the table ):
SELECT s_id, f_name FROM fruits WHERE s_id =(SELECT s1.s_id FROM suppliers AS s1 WHERE s1.s_city = 'Tianjin');
49、 Merge query results ( Query all prices less than 9 Information about fruits , Inquire about s_id be equal to 101 and 103 Information about all fruits , Use UNION ALL Join query results ):
SELECT s_id, f_name, f_price FROM fruits WHERE f_price < 9.0 UNION ALL SELECT s_id, f_name, f_price FROM fruits WHERE s_id IN(101,103);
Aliasing tables and fields
For ease of operation or when you need to use the same table multiple times , You can specify an alias for the table , Use this alias instead of the original name
Alias the table
50、 by orders The name of the table is o, Inquire about 30001 Date of order :
SELECT * FROM orders AS o WHERE o.o_num = 30001;
51、 by customers and orders Tables are aliased respectively , And conduct connection query :
SELECT c.c_id, o.o_num FROM customers AS c LEFT OUTER JOIN orders AS o ON c.c_id = o.c_id;
Alias the field
52、 Inquire about fruits surface , by f_name Take the alias fruit_name,f_price Take the alias fruit_price, by fruits The name of the table is f1, In the query table f_price < 8 The name of the fruit :
SELECT f1.f_name AS fruit_name, f1.f_price AS fruit_price FROM fruits AS f1 WHERE f1.f_price < 8;
Using regular expression queries
Query records starting with a specific character or string ^
Query records ending with a specific character or string $
Use symbols "." To replace any character in the string
Use "*" and "+" To match multiple characters
Matches the specified string
Matches any one of the specified characters
Matches characters other than the specified characters
Use {M} perhaps {M,N} To specify the number of consecutive occurrences of the string
53、 In the table , The query field is written in letters 'b' The first record :
SELECT * FROM Table name WHERE Field REGEXP '^b';
54、 In the table , The query field is written in letters 'y' The record at the end :
SELECT * FROM Table name WHERE Field REGEXP 'y$';
55、 In the table , The query field value contains letters 'a' And 'g' And there is only one letter between two letters :
SELECT * FROM Table name WHERE Field REGEXP 'a.g';
56、 In the table , Query field values with letters 'b' start , And 'b' The letters appear after them 'a' The record of :
SELECT * FROM Table name WHERE Field REGEXP '^ba*';
57、 In the table , Query field values with letters 'b' start , And 'b' The letters appear after them 'a' At least one record :
SELECT * FROM Table name WHERE Field REGEXP '^ba+';
58、 In the table , The query field value contains a string “on” The record of :
SELECT * FROM Table name WHERE Field REGEXP 'on';
59、 In the table , The query field value contains a string “on” perhaps “ap” The record of :
SELECT * FROM Table name WHERE Field REGEXP 'on|ap';
60、 In the table , The find field contains letters 'o' perhaps 't' The record of :
SELECT * FROM Table name WHERE Field REGEXP '[ot]';
61、 In the table , The query field contains letters a~e And number 1~2 Records of characters other than :
SELECT * FROM fruits WHERE f_id REGEXP '[^a-e1-2]';
62、 In the table , Letters appear in the query field value 'x' At least 2 The second record :
SELECT * FROM fruits WHERE f_name REGEXP 'x{2,}';
边栏推荐
- LeetCode 热题 HOT 100 -> 2.两数相加
- 获取两个集合相差数据
- 小程序毕设作品之微信校园浴室预约小程序毕业设计成品(1)开发概要
- MySQL explain (glory Collection Edition)
- Product axure9 English version, using repeater repeater repeater to realize multi-choice and single choice
- Clear the cause of floating and six methods (solve the problem that floating affects the parent element and the global)
- 【ROS进阶篇】第九讲 基于Rviz和Arbotix控制的机器人模型运动
- Soft test - database (2) relational model
- ArcGIS: loading historical remote sensing images
- 使用BigDecimal类型应该避免哪些问题?(荣耀典藏版)
猜你喜欢

借助Elephant&nbsp;Swap打造的ePLATO,背后的高溢价解析

Understand the "next big trend" in the encryption industry - ventures Dao

Alipay applet authorization / obtaining user information

Flex layout learning completed on PC side
![[advanced ROS chapter] Lecture 10 gadf integrated simulation process and examples based on gazebo](/img/d0/5fa2ae7fc7866b7a55cf103da4519d.png)
[advanced ROS chapter] Lecture 10 gadf integrated simulation process and examples based on gazebo

MySQL high availability and master-slave synchronization

MySQL数据库InnoDB存储引擎中的锁机制(荣耀典藏版)

MySQL高可用和主从同步

Lock mechanism in MySQL database InnoDB storage engine (glory Collection Edition)

11 Django basics database operation
随机推荐
ArcGIS: loading historical remote sensing images
数字赋能 创新未来:海丝埃睿迪亮相第五届数字中国建设峰会
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(1)开发概要
四种常见的 POST 提交数据方式
埃睿迪再度亮相数字中国峰会 持续深化用科技守护绿水青山
软考 --- 数据库(2)关系模型
[hcip] routing strategy, strategic routing
Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
获取两个集合相差数据
Xiaomi website homepage big module - small module + navigation (floating case)
53: Chapter 5: develop admin management service: 6: develop [admin administrator exit login, interface]; (one point: when we want to modify a value with a certain coding method, the new value should b
组原必备知识点
Flex development web page instance web side
Please, don't use the command line to configure MySQL master-slave replication. Isn't it fragrant to deploy with urlos interface?
Say yes, I will love you, and I will love you well
【ROS进阶篇】第十讲 基于Gazebo的URDF集成仿真流程及实例
APP如何上架App Store?
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(2)小程序功能
实际工作中,我是如何使用 Postman 做接口测试?
[机缘参悟-53]:阳谋立身,阴谋防身