我有来自4个表的联合所有查询。从4个表中我得到了所有4个表中Original_APN
的总数量。为此,我使用以下查询。
SELECT Original_APN SUM(Quanity) Quanity FROM (
SELECT Original_APN, Quanity FROM hs_apn1
UNION ALL
SELECT Original_APN, Quanity FROM hs_apn2
UNION ALL
SELECT Original_APN, Quanity FROM hs_apn3
UNION ALL
SELECT Original_APN, Quanity FROM hs_apn4
) a
GROUP BY Original_APN LIMIT 0,4
以下是我得到的输出:
+---------------+---------+
| Original_APN | Quanity|
+---------------+---------+
| 0097512135344 | 30.00 |
| 0097512203272 | 2.00 |
| 0097512203296 | 2.00 |
| 0617823119019 | 200.00 |
+---------------+---------+
现在我的要求是我还有另一张桌子价格。在该表中,我有共同的列Original_APN。 IN价格表我有Original_APN,aritkel,product_name和价格。以下是我想要的输出。但它没有加入这个价格表与我上面的联合所有查询。
+---------------+---------+---------+--------------+-------+
| Original_APN | Quanity | Artikel | product_name | price |
+---------------+---------+---------+--------------+-------+
| 0097512135344 | 30.00 | 122 | TEST1 | 11.00 |
| 0097512203272 | 2.00 | 34 | TEST2 | 12.45 |
| 0097512203296 | 2.00 | AX1 | TEST3 | 15.65 |
| 0617823119019 | 200.00 | MN4 | TEST4 | 22.35 |
+---------------+---------+---------+--------------+-------+
答案 0 :(得分:1)
您可以使用表别名,然后加入价格表,如下所示:
SELECT s.Original_APN, s.Quantity, p.Artikel, p.product_name, p.price FROM (
SELECT Original_APN, SUM(Quantity) Quantity FROM (
SELECT Original_APN, Quantity FROM hs_apn1
UNION ALL
SELECT Original_APN, Quantity FROM hs_apn2
UNION ALL
SELECT Original_APN, Quantity FROM hs_apn3
UNION ALL
SELECT Original_APN, Quantity FROM hs_apn4
) a
GROUP BY Original_APN
) s
INNER JOIN
prices p
ON s.Original_APN = p.Original_APN