相关子查询和 GROUP BY
correlated subquery and GROUP BY
我开始学习 Mysql,但我对 相关子查询 的理解有问题,我搜索了定义,它对每一行执行一次,但是说到例子:
DEPOT (N_Dpt, Name_Dpt, City)
STORE (N_Dpt #, N_Product #, Qt_Stored)
PRODUCT (N_Product, Label_Product, Price_U, Weight, Color)
每种产品类型的最高单价列表
SELECT Product_Label, Price_U FROM Product AS P1
WHERE Price = (SELECT MAX (Price_U) FROM PRODUCT P2
WHERE P2. Product_Label = P1. Product Label )
第一个问题:为什么是相关子查询?
第二个问题:为什么我们不能使用 GROUP BY?
SELECT Product_label, MAX(Price_U) FROM PRODUCT GROUP BY Product_label
考虑以下...
DROP TABLE IF EXISTS product;
CREATE TABLE product
(id SERIAL PRIMARY KEY
,product_label CHAR(1) NOT NULL
,price_u INT NOT NULL
);
INSERT INTO product VALUES
(1,'a',12),
(2,'a',18),
(3,'a',4),
(4,'b',9),
(5,'b',1),
(6,'c',13);
-- Correlated subquery:
SELECT product_label
, price_u
FROM product p1
WHERE price_u = (SELECT MAX(price_u) FROM product p2 WHERE p2.product_label = p1.product_label);
-----------------------------------------
+---------------+---------+
| product_label | price_u |
+---------------+---------+
| a | 18 |
| b | 9 |
| c | 13 |
+---------------+---------+
-- Uncorrelated subquery
SELECT x.product_label
, x.price_u
FROM product x
JOIN
( SELECT product_label
, MAX(price_u) price_u
FROM product
GROUP
BY product_label
) y
ON y.product_label = x.product_label
AND y.price_u = x.price_u;
+---------------+---------+
| product_label | price_u |
+---------------+---------+
| a | 18 |
| b | 9 |
| c | 13 |
+---------------+---------+
-- No subquery
SELECT x.product_label
, x.price_u
FROM product x
LEFT
JOIN product y
ON y.product_label = x.product_label
AND y.price_u > x.price_u
WHERE y.id IS NULL;
+---------------+---------+
| product_label | price_u |
+---------------+---------+
| a | 18 |
| b | 9 |
| c | 13 |
+---------------+---------+
以上并非详尽无遗
我开始学习 Mysql,但我对 相关子查询 的理解有问题,我搜索了定义,它对每一行执行一次,但是说到例子:
DEPOT (N_Dpt, Name_Dpt, City)
STORE (N_Dpt #, N_Product #, Qt_Stored)
PRODUCT (N_Product, Label_Product, Price_U, Weight, Color)
每种产品类型的最高单价列表
SELECT Product_Label, Price_U FROM Product AS P1
WHERE Price = (SELECT MAX (Price_U) FROM PRODUCT P2
WHERE P2. Product_Label = P1. Product Label )
第一个问题:为什么是相关子查询?
第二个问题:为什么我们不能使用 GROUP BY?
SELECT Product_label, MAX(Price_U) FROM PRODUCT GROUP BY Product_label
考虑以下...
DROP TABLE IF EXISTS product;
CREATE TABLE product
(id SERIAL PRIMARY KEY
,product_label CHAR(1) NOT NULL
,price_u INT NOT NULL
);
INSERT INTO product VALUES
(1,'a',12),
(2,'a',18),
(3,'a',4),
(4,'b',9),
(5,'b',1),
(6,'c',13);
-- Correlated subquery:
SELECT product_label
, price_u
FROM product p1
WHERE price_u = (SELECT MAX(price_u) FROM product p2 WHERE p2.product_label = p1.product_label);
-----------------------------------------
+---------------+---------+
| product_label | price_u |
+---------------+---------+
| a | 18 |
| b | 9 |
| c | 13 |
+---------------+---------+
-- Uncorrelated subquery
SELECT x.product_label
, x.price_u
FROM product x
JOIN
( SELECT product_label
, MAX(price_u) price_u
FROM product
GROUP
BY product_label
) y
ON y.product_label = x.product_label
AND y.price_u = x.price_u;
+---------------+---------+
| product_label | price_u |
+---------------+---------+
| a | 18 |
| b | 9 |
| c | 13 |
+---------------+---------+
-- No subquery
SELECT x.product_label
, x.price_u
FROM product x
LEFT
JOIN product y
ON y.product_label = x.product_label
AND y.price_u > x.price_u
WHERE y.id IS NULL;
+---------------+---------+
| product_label | price_u |
+---------------+---------+
| a | 18 |
| b | 9 |
| c | 13 |
+---------------+---------+
以上并非详尽无遗