我正在使用5个不同表的联合处理Join表 因为我已经为联合所有表编写了代码,并且它没有连接表而正常工作
你可以看到代码
SELECT sku,
sum(units_ordered) as units_ordered
FROM (
SELECT sku, units_ordered
FROM tbl_sku_units_order
UNION ALL
SELECT sku, units_ordered
FROM tbl_sku_units_order_de
UNION ALL
SELECT sku, units_ordered
FROM tbl_sku_units_order_es
UNION ALL
SELECT sku, units_ordered
FROM tbl_sku_units_order_fr
UNION ALL
SELECT sku, units_ordered
FROM tbl_sku_units_order_it
) AllMarketplace
group by sku
ORDER BY units_ordered DESC;
你可以在db fiddle的第一个链接中看到结果
https://www.db-fiddle.com/f/nbGcxACaf4doHRssJDSyRN/0,结果是正确的
你可以看到" H-Root-M012S-Black 87"这是正确的QTY股票总数。
我需要他们加入其他一个表作为左连接。 这是db fiddle
中的代码https://www.db-fiddle.com/f/dxqsU4enjRYqk7du7VEB4d/0
这是我写的代码
SELECT
ls.sku AS list_sku,
COALESCE(MIN(suo.sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(suo.units_ordered), 0) AS units_ordered
FROM
(SELECT
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order
UNION ALL
SELECT
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_de
UNION ALL
SELECT
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_es
UNION ALL
SELECT
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_fr
UNION ALL
SELECT
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_it
) as t1,
tbl_list_sku AS ls
LEFT JOIN
tbl_sku_units_order AS suo ON suo.sku = ls.sku
GROUP BY ls.sku
ORDER BY units_ordered DESC;
它返回正常,但计算错误
由于团体问题,我怀疑它?我必须在组上调整它,但所有结果都返回相同的结果以及右或内连接表。
答案 0 :(得分:1)
你的SQL应该是这样的:
SELECT
ls.sku AS list_sku,
COALESCE(MIN(suo.sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(suo.units_ordered), 0) AS units_ordered
FROM
(SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order
UNION ALL
SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_de
UNION ALL
SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_es
UNION ALL
SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_fr
UNION ALL
SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_it
) as t1 join
tbl_list_sku AS ls on t1.sku = ls.sku
LEFT JOIN
tbl_sku_units_order AS suo ON suo.sku = ls.sku
GROUP BY ls.sku
ORDER BY units_ordered DESC;
SELECT
ls.sku AS list_sku,
COALESCE(MIN(suo.sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(suo.units_ordered), 0) AS units_ordered
FROM tbl_list_sku AS ls left join
(SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order
UNION ALL
SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_de
UNION ALL
SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_es
UNION ALL
SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_fr
UNION ALL
SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_it
) as t1 on t1.sku = ls.sku
LEFT JOIN
tbl_sku_units_order AS suo ON suo.sku = ls.sku
GROUP BY ls.sku
ORDER BY units_ordered DESC;