我有 3 个表,它们将合并成 1 个表。
表1
id|prdct_name|qty
001 A 5
002 B 5
003 C 5
表2
id|prdct_id|price
1 001 100
2 001 200
3 002 150
4 002 250
表3
id|prdct_id|stock_in|stock_out
1 001 5 0
2 001 10 0
3 002 15 0
4 002 25 0
我已经尝试过这个sql(由于拼写错误而更新)
select a.prdct_name as Name, a.qty as QTY,b.price as PRICE, c.stock_in as SIN,c.stock_out as SOUT
from table1 a
left join table2 b on a.id=b.prdct_id
left join table3 c on a.id=c.prdct_id
where 1=1 group by b.id,c.id
但结果返回重复的内容,如下表所示
NAME|QTY|PRICE|SIN|SOUT
A 5 100 5 0
A 5 100 10 0
A 5 200 5 0
A 5 200 10 0
B 5 150 15 0
B 5 150 25 0
B 5 250 15 0
B 5 250 25 0
结果应该是
NAME|QTY|PRICE|SIN|SOUT
A 5 100 5 0
A 5 200 10 0
B 5 150 15 0
B 5 250 25 0
有没有办法消除重复的问题?尝试使用不同的方法也无济于事。 谢谢
最佳答案
看起来您的第二个 join
应该位于 id
上 - 但您的 join
条件无论如何看起来都不正确。
select a.prdct_name as Name, a.qty as QTY, b.price as PRICE, c.stock_in as SIN,c.stock_out as SOUT
from table1 a left join
table2 b
on a.id = b.prdct_id left join
table3 c
on a.id = c.id
where 1=1
group by b.id, c.id
关于mysql - 如何通过sql从查询组中获取结果而不返回重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52039119/