如何在 MySql 中将查询设置为变量
How to set Query into variable in MySql
我想查找每个组中最便宜和最贵商品的商品名称。但是我不知道为什么我在“SET @maxi := (SELECT A.ItName FROM tblqtdtl B INNER JOIN tblitem A ON A.ItCode = B.ItCode GROUP BY A.ItCode HAVING MAX(B.Price))
”附近有SQL错误(1064)。也许我的 sql 代码
有问题或缺失
这就是我所期待的
vd码
视频名称
最便宜的商品
最便宜的价格
最贵的商品
最贵的价格
Vd001
DEF 纺织品
涤纶
2600
涤纶
2600
Vd003
MNO 纺织
涤纶
3228
丝绸
3950
Vd004
XYZ 纺织
涤纶
2800
丝绸
3480
这是我的代码
SET @mini :=(SELECT A.ItName FROM tblqtdtl B INNER JOIN tblitem A ON A.ItCode = B.ItCode
GROUP BY A.ItCode HAVING MIN(B.Price)
)
SET @maxi := (SELECT A.ItName FROM tblqtdtl B INNER JOIN tblitem A ON A.ItCode = B.ItCode
GROUP BY A.ItCode HAVING MAX(B.Price)
)
SELECT V.VdCode, V.VdName, @mini AS 'Cheapest Item', MIN(B.Price) AS 'Cheapest Price', @maxi AS 'Most Expensive Item'
,MAX(B.Price) AS 'Most Expensive Price'
FROM tblqthdr C
INNER JOIN tblvendor V ON C.VdCode = V.VdCode
INNER JOIN tblqtdtl B ON C.DocNo = B.DocNo
INNER JOIN tblitem A ON B.ItCode = A.ItCode
WHERE V.ActInd='Y'
GROUP BY V.VdCode
这是输入脚本
tblqdtl
INSERT INTO `tblqtdtl` (`DocNo`, `ItCode`, `Price`) VALUES
('QT001', 'It004', 38700.00),
('QT002', 'It002', 25600.00),
('QT003', 'It005', 21000.00),
('QT004', 'It004', 34000.00),
('QT005', 'It003', 32000.00),
('QT006', 'It002', 31780.00),
('QT007', 'It001', 28000.00),
('QT008', 'It002', 21800.00),
('QT009', 'It004', 32600.00),
('QT010', 'It002', 27500.00);
tblitem
INSERT INTO `tblitem` (`ItCode`, `ItName`, `ItCtCode`) VALUES
('Itestt001', 'Cotton', 'ALA'),
('It002', 'Polyester', 'SIN'),
('It003', 'Wool', 'ALA'),
('It004', 'Silk', 'ALA'),
('It005', 'Rayon', 'SIN');
tblqhdr
INSERT INTO `tblqthdr` (`DocNo`, `DocDt`, `VdCode`) VALUES
('QT001', '20180102', 'Vd003'),
('QT002', '20180203', 'Vd001'),
('QT003', '20180203', 'Vd002'),
('QT004', '20180207', 'Vd004'),
('QT005', '20180304', 'Vd003'),
('QT006', '20180401', 'Vd003'),
('QT007', '20180701', 'Vd005'),
('QT008', '20180902', 'Vd002'),
('QT009', '20180902', 'Vd005'),
('QT010', '20181203', 'Vd004');
tblvendor
INSERT INTO `tblvendor` (`VdCode`, `VdName`, `ActInd`) VALUES
('Vd001', 'ABC Textile', 'Y'),
('Vd002', 'DEF Textile', 'N'),
('Vd003', 'GHI Textile', 'Y'),
('Vd004', 'JKL Textile', 'Y'),
('Vd005', 'MNO Textile', 'N');
SELECT DISTINCT
VdCode,
VdName,
FIRST_VALUE(ItName) OVER (PARTITION BY VdCode ORDER BY Price ASC) `Cheapest item`,
MIN(Price) OVER (PARTITION BY VdCode) `Cheapest price`,
FIRST_VALUE(ItName) OVER (PARTITION BY VdCode ORDER BY Price DESC) `Most expensive item`,
MAX(Price) OVER (PARTITION BY VdCode) `Most Expensive Price`
FROM ( SELECT *
FROM tblqthdr C
INNER JOIN tblvendor V USING (VdCode)
INNER JOIN tblqtdtl B USING (DocNo)
INNER JOIN tblitem A USING (ItCode)
WHERE V.ActInd='Y' ) subquery
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=53cade7afe509dcde6f259e5c5ae3fa3
我想查找每个组中最便宜和最贵商品的商品名称。但是我不知道为什么我在“SET @maxi := (SELECT A.ItName FROM tblqtdtl B INNER JOIN tblitem A ON A.ItCode = B.ItCode GROUP BY A.ItCode HAVING MAX(B.Price))
”附近有SQL错误(1064)。也许我的 sql 代码
这就是我所期待的
vd码 | 视频名称 | 最便宜的商品 | 最便宜的价格 | 最贵的商品 | 最贵的价格 |
---|---|---|---|---|---|
Vd001 | DEF 纺织品 | 涤纶 | 2600 | 涤纶 | 2600 |
Vd003 | MNO 纺织 | 涤纶 | 3228 | 丝绸 | 3950 |
Vd004 | XYZ 纺织 | 涤纶 | 2800 | 丝绸 | 3480 |
这是我的代码
SET @mini :=(SELECT A.ItName FROM tblqtdtl B INNER JOIN tblitem A ON A.ItCode = B.ItCode
GROUP BY A.ItCode HAVING MIN(B.Price)
)
SET @maxi := (SELECT A.ItName FROM tblqtdtl B INNER JOIN tblitem A ON A.ItCode = B.ItCode
GROUP BY A.ItCode HAVING MAX(B.Price)
)
SELECT V.VdCode, V.VdName, @mini AS 'Cheapest Item', MIN(B.Price) AS 'Cheapest Price', @maxi AS 'Most Expensive Item'
,MAX(B.Price) AS 'Most Expensive Price'
FROM tblqthdr C
INNER JOIN tblvendor V ON C.VdCode = V.VdCode
INNER JOIN tblqtdtl B ON C.DocNo = B.DocNo
INNER JOIN tblitem A ON B.ItCode = A.ItCode
WHERE V.ActInd='Y'
GROUP BY V.VdCode
这是输入脚本
tblqdtl
INSERT INTO `tblqtdtl` (`DocNo`, `ItCode`, `Price`) VALUES
('QT001', 'It004', 38700.00),
('QT002', 'It002', 25600.00),
('QT003', 'It005', 21000.00),
('QT004', 'It004', 34000.00),
('QT005', 'It003', 32000.00),
('QT006', 'It002', 31780.00),
('QT007', 'It001', 28000.00),
('QT008', 'It002', 21800.00),
('QT009', 'It004', 32600.00),
('QT010', 'It002', 27500.00);
tblitem
INSERT INTO `tblitem` (`ItCode`, `ItName`, `ItCtCode`) VALUES
('Itestt001', 'Cotton', 'ALA'),
('It002', 'Polyester', 'SIN'),
('It003', 'Wool', 'ALA'),
('It004', 'Silk', 'ALA'),
('It005', 'Rayon', 'SIN');
tblqhdr
INSERT INTO `tblqthdr` (`DocNo`, `DocDt`, `VdCode`) VALUES
('QT001', '20180102', 'Vd003'),
('QT002', '20180203', 'Vd001'),
('QT003', '20180203', 'Vd002'),
('QT004', '20180207', 'Vd004'),
('QT005', '20180304', 'Vd003'),
('QT006', '20180401', 'Vd003'),
('QT007', '20180701', 'Vd005'),
('QT008', '20180902', 'Vd002'),
('QT009', '20180902', 'Vd005'),
('QT010', '20181203', 'Vd004');
tblvendor
INSERT INTO `tblvendor` (`VdCode`, `VdName`, `ActInd`) VALUES
('Vd001', 'ABC Textile', 'Y'),
('Vd002', 'DEF Textile', 'N'),
('Vd003', 'GHI Textile', 'Y'),
('Vd004', 'JKL Textile', 'Y'),
('Vd005', 'MNO Textile', 'N');
SELECT DISTINCT
VdCode,
VdName,
FIRST_VALUE(ItName) OVER (PARTITION BY VdCode ORDER BY Price ASC) `Cheapest item`,
MIN(Price) OVER (PARTITION BY VdCode) `Cheapest price`,
FIRST_VALUE(ItName) OVER (PARTITION BY VdCode ORDER BY Price DESC) `Most expensive item`,
MAX(Price) OVER (PARTITION BY VdCode) `Most Expensive Price`
FROM ( SELECT *
FROM tblqthdr C
INNER JOIN tblvendor V USING (VdCode)
INNER JOIN tblqtdtl B USING (DocNo)
INNER JOIN tblitem A USING (ItCode)
WHERE V.ActInd='Y' ) subquery
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=53cade7afe509dcde6f259e5c5ae3fa3