当前位置:网站首页>Don't know how to learn MySQL? It's enough to finish the 50 questions of Niuke! (Part IX)
Don't know how to learn MySQL? It's enough to finish the 50 questions of Niuke! (Part IX)
2022-07-21 18:13:00 【18-year-old hates Java】
This content is divided into ten chapters , Five questions each time , Other chapters are on the homepage Mysql Column
Preface
Are you still worrying SQL Where to start , Or learned SQL I want to find a place to practice my hand ? What a coincidence , Recently, after work, I boarded Cattle guest , Found out Cattle guest I don't know when it went online SQL Will know The exercises of .
《SQL Will know 》 As MIT 、 Erie Reference textbooks of many universities such as neu University , From simple to deep SQL The basic concepts and grammar of . Involving data Sort 、 Filtering and grouping , And the watch 、 View 、 coupling 、 Subquery 、 The cursor 、 Stored procedures and triggers . Examples abound , Convenient access , It can be said that as a CRUD BOY/GIRL A must read list .
Just think about brushing it again , And then put yourself Brush problem To sum up , So here's today's article , I hope I can give a little help to my little friend in need
SQL41 Another way to determine the best customer ( Two )
describe
OrderItems Table represents order information , Another way to determine the best customer is to see how much they spend ,OrderItems The table has the order number order_num and item_price The selling price of the goods 、quantity The number
order_num | item_price | quantity |
---|---|---|
a1 | 10 | 105 |
a2 | 1 | 1100 |
a2 | 1 | 200 |
a4 | 2 | 1121 |
a5 | 5 | 10 |
a2 | 1 | 19 |
a7 | 7 | 5 |
Orders Table contains fields order_num The order number 、cust_id customer id
order_num | cust_id |
---|---|
a1 | cust10 |
a2 | cust1 |
a3 | cust2 |
a4 | cust22 |
a5 | cust221 |
a7 | cust2217 |
Customer watch Customers There are fields cust_id Customer id、cust_name Name of customer
cust_id | cust_name |
---|---|
cust10 | andy |
cust1 | ben |
cust2 | tony |
cust22 | tom |
cust221 | an |
cust2217 | hex |
problem
To write SQL sentence , The total price of the returned order shall not be less than 1000 Customer name and total amount of (OrderItems In the table order_num).
Tips : You need to calculate the sum (item_price multiply quantity). The result is calculated according to the total amount Sort , Please use INNER JOIN grammar .
Sample results
cust_name | total_price |
---|---|
andy | 1050 |
ben | 1319 |
tom | 2242 |
Example analysis
Total (item_price multiply quantity) Greater than or equal to 1000 Order number , for example a2 Corresponding customers id by cust1,cust1 Name of the customer cust_name yes ben, Finally back to ben As order_num a2 Of quantity * item_price The result of the sum 1319.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
answer
The main knowledge points to be investigated :
- Take the alias :
AS
keyword - Sum up :
SUM()
GROUP BY
andHAVING
At the same time, it is used to filter the results- Sort :
ORDER BY
keyword
Mainly investigate multiple SQL Use of keywords , At the same time, we need to pay attention to SQL The sequence of keywords in .
1 2 3 4 5 6 |
|
SQL42 Retrieve the name of each customer and all order numbers ( One )
describe
Customers The table represents customer information and contains customers id cust_id And customer name cust_name
cust_id | cust_name |
---|---|
cust10 | andy |
cust1 | ben |
cust2 | tony |
cust22 | tom |
cust221 | an |
cust2217 | hex |
Orders The table represents the order information, including the order number order_num And customers id cust_id
order_num | cust_id |
---|---|
a1 | cust10 |
a2 | cust1 |
a3 | cust2 |
a4 | cust22 |
a5 | cust221 |
a7 | cust2217 |
problem
Use INNER JOIN To write SQL sentence , Retrieve the name of each customer (Customers In the table cust_name) And all order numbers (Orders In the table order_num), Finally, according to the customer's name cust_name Returns... In ascending order .
Sample results
Return customer name cust_name And order number order_num
cust_name | order_num |
---|---|
an | a5 |
andy | a1 |
ben | a2 |
hex | a7 |
tom | a4 |
tony | a3 |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
answer
Main investigation INNER JOIN
Usage of , Its usage syntax is as follows :
1 2 3 4 |
|
To realize this problem , Use the above syntax to fill in the corresponding field name and table name , The last is the keyword ORDER BY
Use .
1 |
|
SQL43 Retrieve the name of each customer and all order numbers ( Two )
describe
Orders The table represents the order information, including the order number order_num And customers id cust_id
order_num | cust_id |
---|---|
a1 | cust10 |
a2 | cust1 |
a3 | cust2 |
a4 | cust22 |
a5 | cust221 |
a7 | cust2217 |
Customers The table represents customer information and contains customers id cust_id and Customer name cust_name
cust_id | cust_name |
---|---|
cust10 | andy |
cust1 | ben |
cust2 | tony |
cust22 | tom |
cust221 | an |
cust2217 | hex |
cust40 | ace |
problem
Retrieve the name of each customer (Customers In the table cust_name) And all order numbers (Orders In the table order_num), List all customers , Even if they haven't placed an order . Finally, according to the customer's name cust_name Returns... In ascending order .
Sample results
Return customer name cust_name And order number order_num
cust_name | order_num |
---|---|
ace | NULL |
an | a5 |
andy | a1 |
ben | a2 |
hex | a7 |
tom | a4 |
tony | a3 |
Example analysis
Based on two tables , Return order No a1 Name of the customer andy wait forsomeone , Customers who don't place orders ace Also counted in .
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
answer
According to the meaning , Mainly with Customers
The columns in the table are primary , And then take Customers
and Orders
The intersection of . about Orders
If the column does not exist in the table, the value is null
. So you can use... In the extranet LEFT JION
, Its usage syntax is as follows :
1 2 3 4 |
|
Apply the above Syntax , Fill in the corresponding table and column name to achieve , Finally, add simple Sort that will do .
1 |
|
SQL44 Returns the product name and its associated order number
describe
Products The table is a product information table with fields prod_id product id、prod_name The product name
prod_id | prod_name |
---|---|
a0001 | egg |
a0002 | sockets |
a0013 | coffee |
a0003 | cola |
a0023 | soda |
OrderItems The table is the order information table, which contains fields order_num Order number and product id prod_id
prod_id | order_num |
---|---|
a0001 | a105 |
a0002 | a1100 |
a0002 | a200 |
a0013 | a1121 |
a0003 | a10 |
a0003 | a19 |
a0003 | a5 |
problem
Use OUTER JOIN coupling Products Table and OrderItems surface , Return the product name (prod_name) And the order number associated with it (order_num) A list of , And in ascending order according to the product name Sort .
Sample results
Return the product name prod_name And order number order_num
prod_name | order_num |
---|---|
coffee | a1121 |
cola | a5 |
cola | a19 |
cola | a10 |
egg | a105 |
sockets | a200 |
sockets | a1100 |
soda | NULL |
Example analysis
Return the order number of the product and the corresponding actual payment order , But products without actual orders soda Also returned , Finally, according to the product name ascending Sort .
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
answer
The solution of this problem is similar to 43 topic , The main use of OUTER JOIN
Left join in LEFT JOIN
, Mainly clear the relevant syntax , Then apply and fill in the table name and column name .
1 2 3 |
|
SQL45 Return the product name and the total number of orders for each product
describe
Products The table is a product information table with fields prod_id product id、prod_name The product name
prod_id | prod_name |
---|---|
a0001 | egg |
a0002 | sockets |
a0013 | coffee |
a0003 | cola |
a0023 | soda |
OrderItems The table is the order information table, which contains fields order_num Order number and product id prod_id
prod_id | order_num |
---|---|
a0001 | a105 |
a0002 | a1100 |
a0002 | a200 |
a0013 | a1121 |
a0003 | a10 |
a0003 | a19 |
a0003 | a5 |
problem
Use OUTER JOIN coupling Products Table and OrderItems surface , Return the product name (prod_name) And the total number of orders for each product ( Not the order number ), And in ascending order by product name Sort .
Sample results
Return the product name prod_name Order number and order number orders
prod_name | orders |
---|---|
coffee | 1 |
cola | 3 |
egg | 1 |
sockets | 2 |
soda | 0 |
Example analysis
Returns the number of orders actually paid for the product and the corresponding product , But products without actual orders soda Also returned , Finally, according to the product name ascending Sort .
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
answer
The knowledge point of investigation is still external connection , Just joined in COUNT()
The function is used for grouping statistics , Finally, it is also simple Sort .
1 2 3 |
|
边栏推荐
- 狂神redis笔记05
- js svg云朵和爱心动画js特效
- JS chart evaporation precipitation average temperature diagram display
- H5 mobile game run rabbit source code download
- 2022-07-20:以下go语言代码是关于json 和 context的,输出什么?A:{};B:{“a“:“b“};C:{“Context“:0};D:不确定。 package main imp
- STM32F103 LED flashing program realized by four methods
- 解析TCP之滑动窗口
- 使用esp32 download tool给esp32烧录固件
- 复盘:智能座舱系列文一,他到底是什么
- 冒号用法 视频41 13.3. 目标检测和边界框 QA 13.4锚框
猜你喜欢
随机推荐
Overview and deployment of redis (master-slave replication, sentinel mode, cluster)
C language Parallel Programming (1)
网络安全学习(千锋网络安全笔记)3--批处理编写
Memories of the final examination of information security of the Department of software
VIII Practice -- SQL and dynamic SQL
Mobilevit: challenge the end-to-side overlord of mobilenet
Recursive implementation of combinatorial enumeration
UE4 spring blueprint - survival development game
Vi Practice -- import and export of Excel tables
About dynamic setting text of label and button in swing interface
"In order to buy a mobile phone, I still have no choice after half a month of research."
VIM editor learning notes
复盘:智能座舱系列文二:它背后的5种交互技术之视觉
Question 133 of Li Kou: numbers that appear only once
Vite configure CDN load resources
redis 实践(一)实现登录
ipset v7.10: Kernel error received: set type not supported
About web responsive design
Under the wave of infotnews | meta universe, enterprises are improving customer experience through AR and VR technologies
PKI证书体系