当前位置:网站首页>Niuke MySQL - SQL must know and know
Niuke MySQL - SQL must know and know
2022-07-28 07:35:00 【Ink man bookworm】
Preface
Author's brief introduction : Hello, friends , I'm your little bookworm
Personal home page : Ink man bookworm
Series column : The finger of the sword offer
Think the words written by the little poet are good Please use your hands give the thumbs-up Collection Comment on , Thank you
Today's series is :MYsql——SQL Will know
Retrieving data
The first 1 topic
Existing table Customers as follows :
| cust_id |
|---|
| A |
| B |
| C |
【 problem 】
To write SQL sentence , from Customers Retrieve all... From the table cust_id
select cust_id from Customers
The first 2 topic
surface OrderItems Contains non empty Columns prod_id On behalf of goods id, Includes all ordered items ( Some have been ordered many times ).
| prod_id |
|---|
| a1 |
| a2 |
| a3 |
| a4 |
| a5 |
| a6 |
| a7 |
【 problem 】 To write SQL sentence , Retrieve and list all ordered items (prod_id) The list after weight removal .
select distinct prod_id from OrderItems
The first 3 topic
Now there is Customers surface ( The table contains columns cust_id On behalf of the customer id,cust_name Name of representative client )
| cust_id | cust_name |
|---|---|
| a1 | andy |
| a2 | ben |
| a3 | tony |
| a4 | tom |
| a5 | an |
| a6 | lee |
| a7 | hex |
【 problem 】 You need to write SQL sentence , Retrieve all columns .
select cust_id,cust_name from Customers
Sorting and retrieving data
The first 1 topic
Have a watch Customers,cust_id On behalf of the customer id,cust_name Name of representative client .
| cust_id | cust_name |
|---|---|
| a1 | andy |
| a2 | ben |
| a3 | tony |
| a4 | tom |
| a5 | an |
| a6 | lee |
| a7 | hex |
【 problem 】 from Customers Retrieve all customer names in (cust_name), And press from Z To A Display the results in the order of .
select cust_name from Customers order by cust_name desc
The first 2 topic
Yes Orders surface
| cust_id | order_num | order_date |
|---|---|---|
| andy | aaaa | 2021-01-01 00:00:00 |
| andy | bbbb | 2021-01-01 12:00:00 |
| bob | cccc | 2021-01-10 12:00:00 |
| dick | dddd | 2021-01-11 00:00:00 |
【 problem 】 To write SQL sentence , from Orders In the customer search table ID(cust_id) And order number (order_num), And first press the customer ID Sort results , Then arrange the order date in reverse order .
select
cust_id,order_num
from Orders
order by cust_id asc,order_date desc
The first 3 topic
Let's say I have a OrderItems surface
| quantity | item_price |
|---|---|
| 1 | 100 |
| 10 | 1003 |
| 2 | 500 |
【 problem 】
To write SQL sentence , Show OrderItems The quantity in the table (quantity) And the price (item_price), And according to the quantity from more to less 、 Prices are ranked from high to low .
select quantity,item_price from OrderItems order by quantity desc,item_price desc
The first 4 topic
Yes Vendors surface
| vend_name |
|---|
| haidilao |
| Xiaolongkan |
| Big dragon |
【 problem 】 Below SQL Is there a problem with the statement ? Try to change it to correct , Make it work correctly , And return the result according to vend_name In reverse order
SELECT vend_name,
FROM Vendors
ORDER vend_name DESC;
right key
SELECT vend_name FROM Vendors ORDER by vend_name DESC;
Filtering data
The first 1 topic
Have a watch Products
| prod_id | prod_name | prod_price |
|---|---|---|
| a0018 | sockets | 9.49 |
| a0019 | iphone13 | 600 |
| b0018 | gucci t-shirts | 1000 |
【 problem 】 from Products Retrieve products from the table ID(prod_id) Product name (prod_name), Only return the price of 9.49 Dollar products .
【 Sample results 】 return prod_id and prod_name
select prod_id,prod_name from Products where prod_price=9.49
The first 2 topic
Products surface
| prod_id | prod_name | prod_price |
|---|---|---|
| a0018 | sockets | 9.49 |
| a0019 | iphone13 | 600 |
| b0019 | gucci t-shirts | 1000 |
【 problem 】 To write SQL sentence , from Products Retrieve products from the table ID(prod_id) Product name (prod_name), Only return the price of 9 Products of US dollars or higher .
【 Example answer 】 return prod_id goods id and prod_name Name of commodity
select prod_id,prod_name from Products where prod_price>=9
The first 3 topic
Yes Products surface
| prod_id | prod_name | prod_price |
|---|---|---|
| a0011 | egg | 3 |
| a0019 | sockets | 4 |
| b0019 | coffee | 15 |
【 problem 】 To write SQL sentence , return Products All the prices in the table are at 3 Dollar to 6 The name of the product between US dollars (prod_name) And the price (prod_price), Then sort the results by price
【 Sample results 】 Return the product name prod_name And commodity prices prod_price
select
prod_name,
prod_price
from Products
where prod_price between 3 and 6
order by prod_price asc
The first 4 topic
OrderItems The table contains : The order number order_num,quantity Product quantity
| order_num | quantity |
|---|---|
| a1 | 105 |
| a2 | 1100 |
| a2 | 200 |
| a4 | 1121 |
| a5 | 10 |
| a2 | 19 |
| a7 | 5 |
【 problem 】 from OrderItems Retrieve all different and non duplicate order numbers in the table (order_num), Each order must contain 100 One or more products .
【 Sample results 】 Return order number column order_num
select
distinct order_num
from OrderItems
where quantity>=100
Come on together
边栏推荐
- DNA修饰贵金属纳米颗粒|DNA脱氧核糖核酸修饰金属钯Pd纳米颗粒PdNPS-DNA
- 常用电子产品行业标准及认证
- Tutorial (7.0) 06. Zero trust network access ztna * forticlient EMS * Fortinet network security expert NSE 5
- 隔离级别RR、间隙锁、幻读
- 面试中必不可少的性能优化专题~
- Eventbus of guava
- 动态内存管理知识点
- 2018-cvpr-Gesture Recognition: Focus on the Hands
- 登录heroku出现 IP address mismatch的解决方案
- High response ratio first
猜你喜欢

Introduction to magnetic ring selection and EMC rectification skills

guava之guava cache

【google】解决google浏览器不弹出账号密码保存框且无法保存登录信息问题

近红外二区AgzS量子点包裹脱氧核糖核酸DNA|DNA-AgzSQDs(齐岳)

最近最久未使用

DNA修饰金属铑Rh纳米颗粒RhNPS-DNA(DNA修饰贵金属纳米颗粒)

Redis configuration and optimization of NoSQL

js二级联动院系

Reasons why null is not recommended for MySQL fields

教程篇(7.0) 06. 零信任网络访问ZTNA * FortiClient EMS * Fortinet 网络安全专家 NSE 5
随机推荐
The "nuclear bomb level" log4j vulnerability is still widespread and has a continuing impact
EMC设计攻略 —时钟
Deeply analyze the implementation of singleton mode
Don't be afraid of ESD static electricity. This article tells you some solutions
近红外二区AgzS量子点包裹脱氧核糖核酸DNA|DNA-AgzSQDs(齐岳)
Disassemble Huawei switches and learn Basic EMC operations
Redis configuration and optimization of NoSQL
flowable工作流所有业务概念
heroku 操作总结
Current limiting ratelimiter of guava
面试中必不可少的性能优化专题~
2018-cvpr-Gesture Recognition: Focus on the Hands
Use of C3d
和为s的两个数字——每日两题
After learning the four redis cluster solutions at one go, each has its own merits
两个星期学会软件测试?我震惊了!
最早截止时间优先(EDF)
ThreadLocal things
DNA-Ag2SQDs脱氧核糖核酸DNA修饰硫化银Ag2S量子点的合成方法
铜铟硫CuInSe2量子点修饰DNA(脱氧核糖核酸)DNA-CuInSe2QDs(齐岳)