我有两个表:
表1名称:购买
a 01 25
01 25
b 02 75
c 03 <100
c 03 25
表2名称:Sell
b 02 60 br
b 02 40
c 03 80
c 03 <40
d 04 45
d 04 50
01 50(SUM)NULL NULL br NULL
b 02 75 b 02 100(SUM)
c 03 125(SUM)c 03 120(SUM)
NULL NULL NULL
d 04 90(SUM)
当我输入(SUM)时,表示值为2行的SUM
因为这对我来说太难了,所以我需要很多帮助
请帮助我,我不知道该怎么做。非常感谢
而且如果我需要b <70或C <30等条件...
答案 0 :(得分:0)
这有点尴尬,因为MySQL不支持完全外部联接。由于错误而进行编辑:
SELECT B.name, B.name_code, B.total, S.name, S.name_code, S.total
FROM (
SELECT name, name_code, SUM(number) AS total FROM buy GROUP BY name, name_code
) B LEFT JOIN (
SELECT name, name_code, SUM(number) AS total FROM sell GROUP BY name, name_code
) S ON B.name = S.name
UNION
SELECT NULL, NULL, NULL, name, name_code, SUM(number) AS total FROM sell
WHERE name NOT IN (SELECT name FROM buy)
GROUP BY name, name_code;
答案 1 :(得分:0)
SELECT table_1.Name,table_1.Namecode ,SUM(table_1.Number),table_2.Name,
table_2.Namecode,SUM(table_2.Number)
FROM table_1
FULL OUTER JOIN table_2
ON table_1.name = table_2.name
GROUP BY table_1.name,table_2.name