当前位置:网站首页>Task04: set operation
Task04: set operation
2022-06-10 08:49:00 【JxWang05】
Task04: Set operations
Tutorial address
https://github.com/datawhalechina/wonderful-sql
https://gitee.com/datawhalechina/wonderful-sql
1. The addition and subtraction of tables
The addition and subtraction of tables is based on set operation , Think of the intersection 、 Union and subtraction
Generally speaking, it is ,union、intersect、except
1.1 union
1.1.1 Give priority to
union It's Union , Join the same columns , The default is de duplication
I have seen a type of before in the written examination , It means giving priority to the display of data that meets a certain condition
For example, we prefer to show that the selling price is less than 800 The goods
mysql> SELECT product_name,sale_price
-> FROM product
-> WHERE sale_price<800
->
-> UNION
->
-> SELECT product_name,sale_price
-> FROM product
-> WHERE sale_price>=800
-> ;
+--------------+------------+
| product_name | sale_price |
+--------------+------------+
| Punch | 500 |
| Fork | 500 |
| T T-shirt | 1000 |
| motion T T-shirt | 4000 |
| kitchen knife | 3000 |
| pressure cooker | 6800 |
| Clean the board | 880 |
| Ball pen | 5000 |
+--------------+------------+
8 rows in set (0.00 sec)
Of course, you can also merge the same columns from different tables
1.1.2 Consolidated results
Use union, In fact, it's similar to or
That is, the results obtained by merging the two filter conditions
1.1.3 Keep repeating
As mentioned above, the default is de duplication , If we use union all, That is not to heavy
mysql> SELECT product_id, product_name
-> FROM product
-> UNION ALL
-> SELECT product_id, product_name
-> FROM product2;
+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 0001 | T T-shirt |
| 0002 | Punch |
| 0003 | motion T T-shirt |
| 0004 | kitchen knife |
| 0005 | pressure cooker |
| 0006 | Fork |
| 0007 | Clean the board |
| 0008 | Ball pen |
| 0001 | T T-shirt |
| 0002 | Punch |
| 0003 | motion T T-shirt |
| 0009 | glove |
| 0010 | The kettle |
+------------+--------------+
13 rows in set (0.00 sec)
mysql>
1.1.4 Type conversion
When we combine data , The data types of the same column must be the same
But they come from different data sources , So there may be implicit conversions
It should be noted that hive In the middle of join When connected , Avoid implicit data type conversion for associated columns , Otherwise, it is easy to cause data skew
1.2 intersect
intersect It's actually seeking intersection , however mysql At present, it is not supported
We need inner connections to do this
mysql> SELECT p1.product_id, p1.product_name
-> FROM product AS p1
-> INNER JOIN product2 AS p2
-> ON p1.product_id=p2.product_id
-> ;
+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 0001 | T T-shirt |
| 0002 | Punch |
| 0003 | motion T T-shirt |
+------------+--------------+
3 rows in set (0.00 sec)
With product_id Keyword , Take the same part
1.3 except
This difference set ,mysql It is not supported at present
We also need to do it in other ways
For example not in, Select the records that are not in the sub query
That is to say , Subtract subqueries from external queries
mysql> SELECT *
-> FROM product
-> WHERE product_id NOT IN (
-> SELECT product_id FROM product2)
-> ;
+------------+--------------+--------------+------------+----------------+-------------+
| product_id | product_name | product_type | sale_price | purchase_price | regist_date |
+------------+--------------+--------------+------------+----------------+-------------+
| 0004 | kitchen knife | Kitchenware | 3000 | 2800 | 2009-09-20 |
| 0005 | pressure cooker | Kitchenware | 6800 | 5000 | 2009-01-15 |
| 0006 | Fork | Kitchenware | 500 | NULL | 2009-09-20 |
| 0007 | Clean the board | Kitchenware | 880 | 790 | 2008-04-28 |
| 0008 | Ball pen | Office Supplies | 5000 | NULL | 2009-11-11 |
+------------+--------------+--------------+------------+----------------+-------------+
5 rows in set (0.04 sec)
1.4 Symmetry difference
This is a little magical , It refers to two sets subtracting the part of intersection
Again ,mysql Direct syntax is not supported , We need to do it in other ways
Intuitively say , Let's subtract the intersection from each of the two sets , Just merge again
But you need to find the intersection first , Here we can subtract another set directly
It's equivalent to subtracting the intersection , The effect is basically the same
mysql> 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)
-> ;
+------------+--------------+--------------+------------+----------------+-------------+
| product_id | product_name | product_type | sale_price | purchase_price | regist_date |
+------------+--------------+--------------+------------+----------------+-------------+
| 0004 | kitchen knife | Kitchenware | 3000 | 2800 | 2009-09-20 |
| 0005 | pressure cooker | Kitchenware | 6800 | 5000 | 2009-01-15 |
| 0006 | Fork | Kitchenware | 500 | NULL | 2009-09-20 |
| 0007 | Clean the board | Kitchenware | 880 | 790 | 2008-04-28 |
| 0008 | Ball pen | Office Supplies | 5000 | NULL | 2009-11-11 |
| 0009 | glove | clothes | 800 | 500 | NULL |
| 0010 | The kettle | Kitchenware | 2000 | 1700 | 2009-09-20 |
+------------+--------------+--------------+------------+----------------+-------------+
7 rows in set (0.01 sec)
2. Link
2.1 Inner link
It's amazing , I don't think all other places are , Connect , I also used to call it connection
Inner connection is based on keywords , Take the intersection part , Merge the columns of two tables
2.1.1 Normal connection
mysql> SELECT SP.shop_id
-> ,SP.shop_name
-> ,SP.product_id
-> ,P.product_name
-> ,P.product_type
-> ,P.sale_price
-> ,SP.quantity
-> FROM shopproduct AS SP
-> INNER JOIN product AS P
-> ON SP.product_id = P.product_id
-> ;
+---------+-----------+------------+--------------+--------------+------------+----------+
| shop_id | shop_name | product_id | product_name | product_type | sale_price | quantity |
+---------+-----------+------------+--------------+--------------+------------+----------+
| 000A | Tokyo | 0001 | T T-shirt | clothes | 1000 | 30 |
| 000A | Tokyo | 0002 | Punch | Office Supplies | 500 | 50 |
| 000A | Tokyo | 0003 | motion T T-shirt | clothes | 4000 | 15 |
| 000B | Nagoya | 0002 | Punch | Office Supplies | 500 | 30 |
| 000B | Nagoya | 0003 | motion T T-shirt | clothes | 4000 | 120 |
| 000B | Nagoya | 0004 | kitchen knife | Kitchenware | 3000 | 20 |
| 000B | Nagoya | 0006 | Fork | Kitchenware | 500 | 10 |
| 000B | Nagoya | 0007 | Clean the board | Kitchenware | 880 | 40 |
| 000C | Osaka | 0003 | motion T T-shirt | clothes | 4000 | 20 |
| 000C | Osaka | 0004 | kitchen knife | Kitchenware | 3000 | 50 |
| 000C | Osaka | 0006 | Fork | Kitchenware | 500 | 90 |
| 000C | Osaka | 0007 | Clean the board | Kitchenware | 880 | 70 |
| 000D | Fukuoka, | 0001 | T T-shirt | clothes | 1000 | 100 |
+---------+-----------+------------+--------------+--------------+------------+----------+
13 rows in set (0.01 sec)
What we need to pay attention to , When connecting multiple tables , It is better to prefix the table name when selecting columns
There's another problem , It's better to alias the table , Because the original name is usually very long
Then there are some differences between this and the associated subquery , It should be more efficient than subquery
Because the associated subquery is more like , Every time an external query encounters a record , Just go to the internal query results to find
2.1.2 combination where
mysql> SELECT *
-> FROM (-- Step 1: query results
-> SELECT SP.shop_id
-> ,SP.shop_name
-> ,SP.product_id
-> ,P.product_name
-> ,P.product_type
-> ,P.sale_price
-> ,SP.quantity
-> FROM shopproduct AS SP
-> INNER JOIN product AS P
-> ON SP.product_id = P.product_id) AS STEP1
-> WHERE shop_name = ' Tokyo '
-> AND product_type = ' clothes ' ;
+---------+-----------+------------+--------------+--------------+------------+----------+
| shop_id | shop_name | product_id | product_name | product_type | sale_price | quantity |
+---------+-----------+------------+--------------+--------------+------------+----------+
| 000A | Tokyo | 0001 | T T-shirt | clothes | 1000 | 30 |
| 000A | Tokyo | 0003 | motion T T-shirt | clothes | 4000 | 15 |
+---------+-----------+------------+--------------+--------------+------------+----------+
2 rows in set (0.00 sec)
This feeling is to connect the watch first , Then the results are filtered
Of course, we can also choose , Filter the table first , And then connect , for example :
mysql> SELECT SP.shop_id
-> ,SP.shop_name
-> ,SP.product_id
-> ,P.product_name
-> ,P.product_type
-> ,P.sale_price
-> ,SP.quantity
->
-> FROM (-- Subquery 1: from ShopProduct The table filters out the information of Tokyo stores
-> SELECT *
-> FROM shopproduct
-> WHERE shop_name = ' Tokyo ' ) AS SP
->
-> INNER JOIN -- Subquery 2: from Product The table filters out the information of clothing products
->
-> (SELECT *
-> FROM product
-> WHERE product_type = ' clothes ') AS P
->
-> ON SP.product_id = P.product_id;
+---------+-----------+------------+--------------+--------------+------------+----------+
| shop_id | shop_name | product_id | product_name | product_type | sale_price | quantity |
+---------+-----------+------------+--------------+--------------+------------+----------+
| 000A | Tokyo | 0001 | T T-shirt | clothes | 1000 | 30 |
| 000A | Tokyo | 0003 | motion T T-shirt | clothes | 4000 | 15 |
+---------+-----------+------------+--------------+--------------+------------+----------+
2 rows in set (0.00 sec)
There may be errors in the process :
Display all 880 possibilities? (y or n)
This is because it is mixed with tab, It should all be changed to spaces
2.1.3 combination group by
Feeling heel where almost , Connect first , Screening again
mysql> SELECT SP.shop_id
-> ,SP.shop_name
-> ,MAX(P.sale_price) AS max_price
-> FROM shopproduct AS SP
-> INNER JOIN product AS P
-> ON SP.product_id = P.product_id
-> GROUP BY SP.shop_id,SP.shop_name
-> ;
+---------+-----------+-----------+
| shop_id | shop_name | max_price |
+---------+-----------+-----------+
| 000A | Tokyo | 4000 |
| 000B | Nagoya | 4000 |
| 000C | Osaka | 4000 |
| 000D | Fukuoka, | 1000 |
+---------+-----------+-----------+
4 rows in set (0.00 sec)
Then there is the question of thinking , intend , Now there is only the highest price in every store
But I don't know which product is the highest selling price
I feel that I may have to ask for the maximum selling price of each store and its products one by one , Then use union to connect
2.1.4 Natural join
This is ,NATURAL JOIN, Don't have to specify on, Naturally specify keywords
2.2 External links
Outer join may be similar to Union , But it's different
The left link is based on the left , The ones on the right merge into the ones on the left , The right link is the opposite
2.2.1 Left connection
A simple , according to product_id Merge into the left table
mysql> SELECT SP.shop_id
-> ,SP.shop_name
-> ,SP.product_id
-> ,P.product_name
-> ,P.sale_price
-> FROM product AS P
-> LEFT OUTER JOIN shopproduct AS SP
-> ON SP.product_id = P.product_id;
+---------+-----------+------------+--------------+------------+
| shop_id | shop_name | product_id | product_name | sale_price |
+---------+-----------+------------+--------------+------------+
| 000D | Fukuoka, | 0001 | T T-shirt | 1000 |
| 000A | Tokyo | 0001 | T T-shirt | 1000 |
| 000B | Nagoya | 0002 | Punch | 500 |
| 000A | Tokyo | 0002 | Punch | 500 |
| 000C | Osaka | 0003 | motion T T-shirt | 4000 |
| 000B | Nagoya | 0003 | motion T T-shirt | 4000 |
| 000A | Tokyo | 0003 | motion T T-shirt | 4000 |
| 000C | Osaka | 0004 | kitchen knife | 3000 |
| 000B | Nagoya | 0004 | kitchen knife | 3000 |
| NULL | NULL | NULL | pressure cooker | 6800 |
| 000C | Osaka | 0006 | Fork | 500 |
| 000B | Nagoya | 0006 | Fork | 500 |
| 000C | Osaka | 0007 | Clean the board | 880 |
| 000B | Nagoya | 0007 | Clean the board | 880 |
| NULL | NULL | NULL | Ball pen | 5000 |
+---------+-----------+------------+--------------+------------+
15 rows in set (0.00 sec)
mysql> select * from product;
+------------+--------------+--------------+------------+----------------+-------------+
| product_id | product_name | product_type | sale_price | purchase_price | regist_date |
+------------+--------------+--------------+------------+----------------+-------------+
| 0001 | T T-shirt | clothes | 1000 | 500 | 2009-09-20 |
| 0002 | Punch | Office Supplies | 500 | 320 | 2009-09-11 |
| 0003 | motion T T-shirt | clothes | 4000 | 2800 | NULL |
| 0004 | kitchen knife | Kitchenware | 3000 | 2800 | 2009-09-20 |
| 0005 | pressure cooker | Kitchenware | 6800 | 5000 | 2009-01-15 |
| 0006 | Fork | Kitchenware | 500 | NULL | 2009-09-20 |
| 0007 | Clean the board | Kitchenware | 880 | 790 | 2008-04-28 |
| 0008 | Ball pen | Office Supplies | 5000 | NULL | 2009-11-11 |
+------------+--------------+--------------+------------+----------------+-------------+
8 rows in set (0.00 sec)
mysql> select * from shopproduct;
+---------+-----------+------------+----------+
| shop_id | shop_name | product_id | quantity |
+---------+-----------+------------+----------+
| 000A | Tokyo | 0001 | 30 |
| 000A | Tokyo | 0002 | 50 |
| 000A | Tokyo | 0003 | 15 |
| 000B | Nagoya | 0002 | 30 |
| 000B | Nagoya | 0003 | 120 |
| 000B | Nagoya | 0004 | 20 |
| 000B | Nagoya | 0006 | 10 |
| 000B | Nagoya | 0007 | 40 |
| 000C | Osaka | 0003 | 20 |
| 000C | Osaka | 0004 | 50 |
| 000C | Osaka | 0006 | 90 |
| 000C | Osaka | 0007 | 70 |
| 000D | Fukuoka, | 0001 | 100 |
+---------+-----------+------------+----------+
13 rows in set (0.00 sec)
We can see , The contents of the left table are preserved , Such as pressure cooker and ballpoint pen
These two things , It's only in the middle of the left , But their keywords ,product_id, There is no reservation
2.2.2 combination where
There's nothing to say about this , Just connect the watch first , Screening again
2.2.3 Full outer join
outer join, Both left and right tables are reserved , however mysql Currently not supported
2.3 Multiple table joins
We already have a watch
mysql> select * from inventoryproduct;
+--------------+------------+--------------------+
| inventory_id | product_id | inventory_quantity |
+--------------+------------+--------------------+
| P001 | 0001 | 0 |
| P001 | 0002 | 120 |
| P001 | 0003 | 200 |
| P001 | 0004 | 3 |
| P001 | 0005 | 0 |
| P001 | 0006 | 99 |
| P001 | 0007 | 999 |
| P001 | 0008 | 200 |
| P002 | 0001 | 10 |
| P002 | 0002 | 25 |
| P002 | 0003 | 34 |
| P002 | 0004 | 19 |
| P002 | 0005 | 99 |
| P002 | 0006 | 0 |
| P002 | 0007 | 0 |
| P002 | 0008 | 18 |
+--------------+------------+--------------------+
16 rows in set (0.00 sec)
2.3.1 Multiple intra table connections
Check the inventory of each item in each store
mysql> SELECT SP.shop_id
-> ,SP.shop_name
-> ,SP.product_id
-> ,P.product_name
-> ,P.sale_price
-> ,IP.inventory_quantity
-> FROM shopproduct AS SP
-> INNER JOIN product AS P
-> ON SP.product_id = P.product_id
-> INNER JOIN inventoryproduct AS IP
-> ON SP.product_id = IP.product_id
-> WHERE IP.inventory_id = 'P001';
+---------+-----------+------------+--------------+------------+--------------------+
| shop_id | shop_name | product_id | product_name | sale_price | inventory_quantity |
+---------+-----------+------------+--------------+------------+--------------------+
| 000A | Tokyo | 0001 | T T-shirt | 1000 | 0 |
| 000A | Tokyo | 0002 | Punch | 500 | 120 |
| 000A | Tokyo | 0003 | motion T T-shirt | 4000 | 200 |
| 000B | Nagoya | 0002 | Punch | 500 | 120 |
| 000B | Nagoya | 0003 | motion T T-shirt | 4000 | 200 |
| 000B | Nagoya | 0004 | kitchen knife | 3000 | 3 |
| 000B | Nagoya | 0006 | Fork | 500 | 99 |
| 000B | Nagoya | 0007 | Clean the board | 880 | 999 |
| 000C | Osaka | 0003 | motion T T-shirt | 4000 | 200 |
| 000C | Osaka | 0004 | kitchen knife | 3000 | 3 |
| 000C | Osaka | 0006 | Fork | 500 | 99 |
| 000C | Osaka | 0007 | Clean the board | 880 | 999 |
| 000D | Fukuoka, | 0001 | T T-shirt | 1000 | 0 |
+---------+-----------+------------+--------------+------------+--------------------+
13 rows in set (0.00 sec)
The same is to connect the tables first , Screening again
2.3.1 Multiple off table connections
Change the key words , The others are no different
2.4 Non equivalent connection
This probably means , We should choose products that are more expensive than our own , Then count , As the ranking of the product
mysql> SELECT product_id
-> ,product_name
-> ,sale_price
-> ,COUNT(p2_id) AS my_rank
-> -- ,P2_id
-> FROM (
-> SELECT P1.product_id
-> ,P1.product_name
-> ,P1.sale_price
-> ,P2.product_id AS P2_id
-> ,P2.product_name AS P2_name
-> ,P2.sale_price AS P2_price
-> FROM product AS P1
-> LEFT OUTER JOIN product AS P2
-> ON P1.sale_price <= P2.sale_price
-> ) AS X
-> GROUP BY product_id, product_name, sale_price
-> ORDER BY my_rank
-> ;
+------------+--------------+------------+---------+
| product_id | product_name | sale_price | my_rank |
+------------+--------------+------------+---------+
| 0005 | pressure cooker | 6800 | 1 |
| 0008 | Ball pen | 5000 | 2 |
| 0003 | motion T T-shirt | 4000 | 3 |
| 0004 | kitchen knife | 3000 | 4 |
| 0001 | T T-shirt | 1000 | 5 |
| 0007 | Clean the board | 880 | 6 |
| 0002 | Punch | 500 | 8 |
| 0006 | Fork | 500 | 8 |
+------------+--------------+------------+---------+
8 rows in set (0.00 sec)
Anyway, the connection condition depends on on Specified judgment , Adjust to be greater than
Then say, for example, ballpoint pens , It has two items higher than its price
So on a table in the middle , It is connected with the records of these two commodities
for example :
mysql> SELECT product_id
-> ,product_name
-> ,sale_price
-> -- ,COUNT(p2_id) AS my_rank
-> ,P2_id
-> FROM (
-> SELECT P1.product_id
-> ,P1.product_name
-> ,P1.sale_price
-> ,P2.product_id AS P2_id
-> ,P2.product_name AS P2_name
-> ,P2.sale_price AS P2_price
-> FROM product AS P1
-> LEFT OUTER JOIN product AS P2
-> ON P1.sale_price <= P2.sale_price
-> ) AS X
-> -- GROUP BY product_id, product_name, sale_price
-> -- ORDER BY my_rank
-> ;
+------------+--------------+------------+-------+
| product_id | product_name | sale_price | P2_id |
+------------+--------------+------------+-------+
| 0001 | T T-shirt | 1000 | 0008 |
| 0001 | T T-shirt | 1000 | 0005 |
| 0001 | T T-shirt | 1000 | 0004 |
| 0001 | T T-shirt | 1000 | 0003 |
| 0001 | T T-shirt | 1000 | 0001 |
| 0002 | Punch | 500 | 0008 |
| 0002 | Punch | 500 | 0007 |
| 0002 | Punch | 500 | 0006 |
| 0002 | Punch | 500 | 0005 |
| 0002 | Punch | 500 | 0004 |
| 0002 | Punch | 500 | 0003 |
| 0002 | Punch | 500 | 0002 |
| 0002 | Punch | 500 | 0001 |
| 0003 | motion T T-shirt | 4000 | 0008 |
| 0003 | motion T T-shirt | 4000 | 0005 |
| 0003 | motion T T-shirt | 4000 | 0003 |
| 0004 | kitchen knife | 3000 | 0008 |
| 0004 | kitchen knife | 3000 | 0005 |
| 0004 | kitchen knife | 3000 | 0004 |
| 0004 | kitchen knife | 3000 | 0003 |
| 0005 | pressure cooker | 6800 | 0005 |
| 0006 | Fork | 500 | 0008 |
| 0006 | Fork | 500 | 0007 |
| 0006 | Fork | 500 | 0006 |
| 0006 | Fork | 500 | 0005 |
| 0006 | Fork | 500 | 0004 |
| 0006 | Fork | 500 | 0003 |
| 0006 | Fork | 500 | 0002 |
| 0006 | Fork | 500 | 0001 |
| 0007 | Clean the board | 880 | 0008 |
| 0007 | Clean the board | 880 | 0007 |
| 0007 | Clean the board | 880 | 0005 |
| 0007 | Clean the board | 880 | 0004 |
| 0007 | Clean the board | 880 | 0003 |
| 0007 | Clean the board | 880 | 0001 |
| 0008 | Ball pen | 5000 | 0008 |
| 0008 | Ball pen | 5000 | 0005 |
+------------+--------------+------------+-------+
37 rows in set (0.00 sec)
We can see that what is higher than the ballpoint pen is 8 Number and 5 Item No
They are respectively connected with these two records
2.5 Cross connect
cross join, It sounds like outer join, But it's different
This more , Make a connection for matching and mismatching
mysql> SELECT SP.shop_id
-> ,SP.shop_name
-> ,SP.product_id
-> ,P.product_name
-> ,P.sale_price
-> FROM shopproduct AS SP
-> CROSS JOIN product AS P;
+---------+-----------+------------+--------------+------------+
| shop_id | shop_name | product_id | product_name | sale_price |
+---------+-----------+------------+--------------+------------+
| 000A | Tokyo | 0001 | Ball pen | 5000 |
......
+---------+-----------+------------+--------------+------------+
104 rows in set (0.00 sec)
You can also choose to connect implicitly
SELECT SP.shop_id
,SP.shop_name
,SP.product_id
,P.product_name
,P.sale_price
FROM ShopProduct AS SP , Product AS P;
Cross connect is also called Cartesian product , It's the one that's paired one by one
Internal and external connections , In fact, it is the restriction and filtering of cross connection
2.6 Grammar problem
We mentioned above , Cross connect has implicit locality
If this method is adopted , That code may become unreadable
And with where after , It is more difficult to judge whether the result is internal connection or external connection
A. Exercises
A.1
find product and product2 The middle price is higher than 500 The basic information of the products .
mysql> SELECT *
-> FROM product
-> WHERE sale_price>500
->
-> UNION
->
-> SELECT *
-> FROM product2
-> WHERE sale_price>500
-> ;
+------------+--------------+--------------+------------+----------------+-------------+
| product_id | product_name | product_type | sale_price | purchase_price | regist_date |
+------------+--------------+--------------+------------+----------------+-------------+
| 0001 | T T-shirt | clothes | 1000 | 500 | 2009-09-20 |
| 0003 | motion T T-shirt | clothes | 4000 | 2800 | NULL |
| 0004 | kitchen knife | Kitchenware | 3000 | 2800 | 2009-09-20 |
| 0005 | pressure cooker | Kitchenware | 6800 | 5000 | 2009-01-15 |
| 0007 | Clean the board | Kitchenware | 880 | 790 | 2008-04-28 |
| 0008 | Ball pen | Office Supplies | 5000 | NULL | 2009-11-11 |
| 0009 | glove | clothes | 800 | 500 | NULL |
| 0010 | The kettle | Kitchenware | 2000 | 1700 | 2009-09-20 |
+------------+--------------+--------------+------------+----------------+-------------+
8 rows in set (0.01 sec)
A.2
With the help of the implementation of symmetrical difference , seek product and product2 Intersection .
mysql> SELECT *
-> FROM product
-> WHERE product_id IN (SELECT product_id FROM product2)
->
-> UNION
->
-> SELECT *
-> FROM product2
-> WHERE product_id IN (SELECT product_id FROM product)
-> ;
+------------+--------------+--------------+------------+----------------+-------------+
| product_id | product_name | product_type | sale_price | purchase_price | regist_date |
+------------+--------------+--------------+------------+----------------+-------------+
| 0001 | T T-shirt | clothes | 1000 | 500 | 2009-09-20 |
| 0002 | Punch | Office Supplies | 500 | 320 | 2009-09-11 |
| 0003 | motion T T-shirt | clothes | 4000 | 2800 | NULL |
+------------+--------------+--------------+------------+----------------+-------------+
3 rows in set (0.00 sec)
A.3
Which stores are selling the highest priced goods in each category ?
mysql> SELECT p1.product_id, p1.product_name, p1.product_type, p1.sale_price, p2.max_sale_price, sp.shop_name
-> -- SELECT *
-> FROM product AS p1
-> INNER JOIN
-> (SELECT product_type,MAX(sale_price) AS max_sale_price FROM product GROUP BY product_type) AS p2
-> ON p1.product_type = p2.product_type
-> -- ;
-> INNER JOIN
-> shopproduct AS sp
-> ON sp.product_id = p1.product_id
-> -- ;
-> WHERE p1.sale_price = p2.max_sale_price
-> ;
+------------+--------------+--------------+------------+----------------+-----------+
| product_id | product_name | product_type | sale_price | max_sale_price | shop_name |
+------------+--------------+--------------+------------+----------------+-----------+
| 0003 | motion T T-shirt | clothes | 4000 | 4000 | Tokyo |
| 0003 | motion T T-shirt | clothes | 4000 | 4000 | Nagoya |
| 0003 | motion T T-shirt | clothes | 4000 | 4000 | Osaka |
+------------+--------------+--------------+------------+----------------+-----------+
3 rows in set (0.01 sec)
This may seem a little strange , Let's take a look at other things :
mysql> -- SELECT p1.product_id, p1.product_name, p1.product_type, p1.sale_price, p2.max_sale_price, sp.shop_name
mysql> SELECT *
-> FROM product AS p1
-> INNER JOIN
-> (SELECT product_type,MAX(sale_price) AS max_sale_price FROM product GROUP BY product_type) AS p2
-> ON p1.product_type = p2.product_type
-> -- ;
-> -- INNER JOIN
-> -- shopproduct AS sp
-> -- ON sp.product_id = p1.product_id
-> -- ;
-> WHERE p1.sale_price = p2.max_sale_price
-> ;
+------------+--------------+--------------+------------+----------------+-------------+--------------+----------------+
| product_id | product_name | product_type | sale_price | purchase_price | regist_date | product_type | max_sale_price |
+------------+--------------+--------------+------------+----------------+-------------+--------------+----------------+
| 0003 | motion T T-shirt | clothes | 4000 | 2800 | NULL | clothes | 4000 |
| 0005 | pressure cooker | Kitchenware | 6800 | 5000 | 2009-01-15 | Kitchenware | 6800 |
| 0008 | Ball pen | Office Supplies | 5000 | NULL | 2009-11-11 | Office Supplies | 5000 |
+------------+--------------+--------------+------------+----------------+-------------+--------------+----------------+
3 rows in set (0.00 sec)
mysql> select * from shopproduct;
+---------+-----------+------------+----------+
| shop_id | shop_name | product_id | quantity |
+---------+-----------+------------+----------+
| 000A | Tokyo | 0001 | 30 |
| 000A | Tokyo | 0002 | 50 |
| 000A | Tokyo | 0003 | 15 |
| 000B | Nagoya | 0002 | 30 |
| 000B | Nagoya | 0003 | 120 |
| 000B | Nagoya | 0004 | 20 |
| 000B | Nagoya | 0006 | 10 |
| 000B | Nagoya | 0007 | 40 |
| 000C | Osaka | 0003 | 20 |
| 000C | Osaka | 0004 | 50 |
| 000C | Osaka | 0006 | 90 |
| 000C | Osaka | 0007 | 70 |
| 000D | Fukuoka, | 0001 | 100 |
+---------+-----------+------------+----------+
13 rows in set (0.00 sec)
Oh , The other two highest priced items , There is no shop for , Namely 5 and 8
A.4
Use the inner link and the associated sub link to query the goods with the highest selling price in each category .
Um. , Internal connection , It seems that the above has been done , First create a sub table , And then connect them
mysql> SELECT *
-> FROM product AS p1
-> INNER JOIN
-> (SELECT product_type,MAX(sale_price) AS max_sale_price FROM product GROUP BY product_type) AS p2
-> ON p1.product_type = p2.product_type
-> WHERE p1.sale_price = p2.max_sale_price
-> ;
+------------+--------------+--------------+------------+----------------+-------------+--------------+----------------+
| product_id | product_name | product_type | sale_price | purchase_price | regist_date | product_type | max_sale_price |
+------------+--------------+--------------+------------+----------------+-------------+--------------+----------------+
| 0003 | motion T T-shirt | clothes | 4000 | 2800 | NULL | clothes | 4000 |
| 0005 | pressure cooker | Kitchenware | 6800 | 5000 | 2009-01-15 | Kitchenware | 6800 |
| 0008 | Ball pen | Office Supplies | 5000 | NULL | 2009-11-11 | Office Supplies | 5000 |
+------------+--------------+--------------+------------+----------------+-------------+--------------+----------------+
3 rows in set (0.00 sec)
The associated sub query is as follows :
mysql> SELECT *
-> FROM product AS p1
-> WHERE sale_price = (
-> SELECT MAX(sale_price)
-> FROM product AS p2
-> WHERE p1.product_type = p2.product_type
-> GROUP BY product_type)
-> ;
+------------+--------------+--------------+------------+----------------+-------------+
| product_id | product_name | product_type | sale_price | purchase_price | regist_date |
+------------+--------------+--------------+------------+----------------+-------------+
| 0003 | motion T T-shirt | clothes | 4000 | 2800 | NULL |
| 0005 | pressure cooker | Kitchenware | 6800 | 5000 | 2009-01-15 |
| 0008 | Ball pen | Office Supplies | 5000 | NULL | 2009-11-11 |
+------------+--------------+--------------+------------+----------------+-------------+
3 rows in set (0.00 sec)
A.5
Use associated sub query to realize : stay product In the table , Take out product_id, product_name, sale_price, And sort according to the selling price of goods from low to high 、 Add up the selling price .
mysql> SELECT p1.product_id, p1.product_name, p1.sale_price,
-> (
-> SELECT SUM(DISTINCT(CASE WHEN p2.sale_price < p1.sale_price THEN p2.sale_price ELSE 0 END)) + p1.sale_price
-> FROM product AS p2
-> -- WHERE p2.sale_price < p1.sale_price
-> ) AS cnt_price
-> FROM product AS p1
-> ORDER BY p1.sale_price
-> ;
+------------+--------------+------------+-----------+
| product_id | product_name | sale_price | cnt_price |
+------------+--------------+------------+-----------+
| 0002 | Punch | 500 | 500 |
| 0006 | Fork | 500 | 500 |
| 0007 | Clean the board | 880 | 1380 |
| 0001 | T T-shirt | 1000 | 2380 |
| 0004 | kitchen knife | 3000 | 5380 |
| 0003 | motion T T-shirt | 4000 | 9380 |
| 0008 | Ball pen | 5000 | 14380 |
| 0005 | pressure cooker | 6800 | 21180 |
+------------+--------------+------------+-----------+
8 rows in set (0.00 sec)
I don't know what this accumulation means
My way here is , The cumulative price of the product is less than or equal to , Each price is calculated only once
ah , I probably got the idea , When learning window functions
The above result should be wrong in the second line , Its cumulative value should be 1000
I feel that after sorting , A new sequence number column should be added
Then let each line accumulate the selling price less than or equal to its serial number
边栏推荐
- Rendercylinder lights for VTK learning
- js通过身份证号获取出生年月日,性别,年龄
- 零基础转行软件测试需要学到什么程度才能找工作
- Elementary knowledge of optical flow method
- Oracle SQL command line (II. View (2))
- vtk学习之Pipeline管线
- STM32 MPU 开发者的十大工作准则
- 对线HR_MySQL逻辑架构?就这?
- How to make internal interfaces visible to MOQ- How to do internal interfaces visible for Moq?
- 乐鑫对 Zephyr 的最新支持
猜你喜欢

从零到一,一站式解决Linux环境下的MySQL(下载篇)

Credit card customer churn forecast

How knowledge atlas, graph data platform and graph technology help the rapid development of retail industry

Add cache like silk to optimize service

Hospital blood bank management system source code blood bank source code blood bank management source code hospital source code

vtk学习之Pipeline管线

Reference counting and smart pointer for VTK learning

对线HR_MySQL存储引擎,原来是这样啊

vtk学习之坐标系统

vtk学习之PipelineExecute管线的执行过程
随机推荐
MMSegmention系列之三(基本的网络架构和预训练模型)
顶流编辑器 Atom,将于 12 月 15 日退出历史舞台
R language uses the pyramid function of epidisplay package to visualize the pyramid diagram
Grafana启动失败报错:Grafana-server Init Failed: Could not find config defaults, make sure homepath command
Qt sqlite操作笔记
Yaml basic syntax
The R language uses the PDF function to save the visual image results to the PDF file, uses the PDF function to open the image device, and uses the dev.off function to close the image device
MMSegmention系列之五(自定义模型)
C#入门系列(十) -- 一维数组
高通平台中gpio简单操作和调试
window11 无法打开安全中心解决方法
If you want to change careers, why do you prefer software testing?
Mmsegment Series III (basic network architecture and pre training model)
Note taking software: goodnotes 5 Chinese version
Oracle SQL command line (II. View)
Mmsegment Series III (basic network architecture and pre training model)
Video | Le Xin R & D theory
STM32 MPU 开发者的十大工作准则
跳过51单片机,直接学STM32有什么严重后果
How knowledge atlas, graph data platform and graph technology help the rapid development of retail industry