当前位置:网站首页>SQL programming task04 job - set operation
SQL programming task04 job - set operation
2022-06-23 01:21:00 【Shallow look】
List of articles
1 Learning content
2 The addition and subtraction of tables
What is set operation ?
Sets represent... In the field of mathematics “ The sum of all kinds of things ”, Represent in the database domain A collection of records . say concretely , surface 、 The execution results of views and queries are collections of records , The elements are each row in the table or query results . In standard SQL in , Use... For the search results respectively UNION, INTERSECT, EXCEPT To search the results and , Intersection and difference operations , image UNION,INTERSECT,EXCEPT This kind of operator used for set operation is called set operator .
2.1 Addition of tables UNION
2.1.1 UNION
SQL sentence :( Represents the union of two sets )
SELECT product_id, product_name
FROM Product
UNION
SELECT product_id, product_name
FROM Product2;
notes :UNION Such set operators usually remove duplicate records
2.1.2 UNION And OR The predicate
SQL sentence :( The results are consistent )
-- Use OR The predicate
SELECT *
FROM Product
WHERE sale_price / purchase_price < 1.3
OR sale_price / purchase_price IS NULL;
**-- Use UNION
SELECT *
FROM Product
WHERE sale_price / purchase_price < 1.3
UNION
SELECT *
FROM Product
WHERE sale_price / purchase_price IS NULL;
**
2.1.3 UNION ALL
Set operations that contain duplicate rows , stay UNION After add ALL that will do
2.2 MySQL 8.0 Intersection operations are not supported INTERSECT
2.3 Difference set , Complement and subtraction of tables
2.3.1 MySQL 8.0 Not yet EXCEPT operation
MySQL 8.0 Table subtraction operator is not yet supported EXCEPT. But with the help of NOT IN The predicate , We can also implement table subtraction .
Is only found in Product Table but does not exist in Product2 surface :
-- Use IN Clause
SELECT *
FROM Product
WHERE product_id NOT IN (SELECT product_id
FROM Product2)
2.3.2 INTERSECT And AND The predicate
For two query results of the same table , Their friendship INTERSECT In fact, the retrieval conditions of the two queries can be used equivalently AND Predicate join to implement .
2.4 Symmetry difference
Two sets A,B The symmetry difference refers to those that belong only to A Or only belong to B A collection of elements . The intersection of two sets can be regarded as the intersection of two sets, and the symmetry difference between the two sets is removed .
But because in MySQL8.0 in , Because the of two tables or query results cannot be obtained directly . Therefore, it is not suitable to use the above idea to calculate the symmetry difference . Fortunately, there are difference set operations that can be used . You can see intuitively , The difference in symmetry between two sets is equal to A-B And go up B-A, Therefore, in practice, we can use this idea to find the symmetry difference .
Use Product Table and Product2 Table symmetry difference to query which products are only in one of the tables
SQL sentence :
-- Use NOT IN Implement the difference set of two tables
SELECT *
FROM Product
WHERE product_id NOT IN (SELECT product_id FROM Product2)
UNION
SELECT *
FROM Product2
WHERE product_id NOT IN (SELECT product_id FROM Product)
3 Link (JOIN)
Time issues , The grammar rules will not be sorted out for the time being , Subsequent complement .
Details refer to :
DataWhale SQL Team learning
MySQL- - Set operations
4 Exercises
4.1
find product and product2 The middle price is higher than 500 The basic information of the products .
SQL sentence :
select *
from product
where sale_price > 500
union
select *
from product2
where sale_price > 500;
Running results :
4.2
With the help of the implementation of symmetrical difference , seek product and product2 Intersection .
SQL sentence :
select *
from (select * from product
union
select * from product2) as p
where product_id not in
(SELECT product_id FROM product
WHERE product_id NOT IN (SELECT product_id FROM product2)
UNION
SELECT product_id FROM product2
WHERE product_id NOT IN (SELECT product_id FROM product));
Running results :
4.3
Which stores are selling the highest priced goods in each category ?
SQL sentence :
select p1.shop_id,p1.shop_name,p1.quantity,
p2.product_id,p2.product_name,p2.product_type,p2.sale_price,
mp.max_price as ' The highest selling price of this kind of goods is '
from shopproduct as p1
inner join product as p2
on p1.product_id = p2.product_id
inner join (select product_type ,max(sale_price)as max_price
from product
group by product_type
)as mp
on mp.product_type = p2.product_type
and p2.sale_price = mp.max_price;
Running results :
4.4
Use the inner link and the associated sub link to query the goods with the highest selling price in each category .
Inner link SQL sentence :( Consistent with the above question )
select p.product_id,p.product_id,p.product_type,p.sale_price,
mp.max_price as ' The maximum price of this kind of commodity '
from product as p
inner join(select product_type,max(sale_price) as max_price
from product
group by product_type)as mp
on p.product_type = mp.product_type
and p.sale_price = mp.max_price;
Running results :
Associated subquery SQL sentence :
select p.product_id,p.product_type,p.sale_price,
mp.max_price as ' The maximum price of this kind of commodity '
from product as p,
(select product_type,max(sale_price) as max_price
from product
group by product_type) as mp
where p.product_type = mp.product_type
and p.sale_price = mp.max_price;

4.5
Use associated sub query to realize : stay product In the table , Take out product_id, produc_name, slae_price, And sort according to the selling price of goods from low to high 、 Add up the selling price .
SQL sentence :
SELECT p.product_id, p.product_name, p.sale_price,
(select sum(sale_price) from product as p1
where p.sale_price > p1.sale_price
or(p.sale_price=p1.sale_price)
)as ' Cumulative sum '
from product as p
order by sale_price;
Running results :
Exercise reference :
Tianchi Dragon Ball SQL Training camp routine task4 Clock in
边栏推荐
猜你喜欢

【滑动窗口】leetcode992. Subarrays with K Different Integers

I've been outsourcing for four years, but I feel it's useless

Daily question brushing record (I)

A hundred lines of code to realize reliable delay queue based on redis

Sélecteur de hiérarchie

SAP ui5 application development tutorial 103 - how to consume third-party libraries in SAP ui5 applications

SAP mm me27 create intra company sto order

three. JS simulated driving tour art exhibition hall - creating super camera controller

BGP federal comprehensive experiment

cadence SPB17.4 - 中文UI设置
随机推荐
OSPF comprehensive experiment
Ansible learning summary (8) -- Summary of ansible control right raising related knowledge
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
Project directory navigation
How about precious metal spot silver?
[UVM] don't say that your VIP can't use ral model
Tidb monitoring upgrade: a long way to solve panic
Software construction course ADT and OOP understanding
62. 不同路径
It's still like this
Dual cross domain: access allow origin header contains multiple values "*, *", but only one is allowed
Cadence spb17.4 - Chinese UI settings
Flowable global listener monitors the start and end of a process
Template specialization template <>
Hierarchy selector
Is it safe for CICC securities to open an account? What is its relationship with CICC?
leetcode 91. Decode Ways 解码方法(中等)
启牛学堂属于证券公司吗?开户安全吗?
SAP ui5 application development tutorial 102 - detailed explanation of the print function of SAP ui5 applications
Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe