MYSQL 双表联合查询,在使用SUM求和函数同时,如何去除重复数据

cenb03 2008-06-19 11:37:19
例如有两个表
a:

|order|color|sizx|qty|
|is-01| red | s | 5 |
|is-01| red | m | 10|
|is-01| red | l | 15|
|is-01| blue| s | 5 |
|is-01| blue| m | 10|
|is-01| blue| l | 15|

b:

| po |order|color|sizx|qty|
|po-01|is-01| red | s | 5 |
|po-01|is-01| red | l | 15|
|po-01|is-01| blue| s | 5 |
|po-02|is-01| red | m | 10|
|po-02|is-01| blue| m | 10|
|po-02|is-01| blue| l | 15|
想要查询结果如下:
|order|color|qty| po |
|is-01| red | 30| po-01,po-02 |
|is-01| bule| 30| po-01,po-02 |

请问该SQL如何写,MYSQL有字符函数可以实现|PO-01,PO-02|吗?
另外,我的MYSQL版本不支持多重查询

...全文
2299 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwwb 2008-06-19
  • 打赏
  • 举报
回复
select a.order,a.color,sum(a.qty),group_concat(distinct b.po SEPARATOR ',')
from a left join b
on a.order=b.order and a.color=b.color
group by a.order,a.color
wwwwb 2008-06-19
  • 打赏
  • 举报
回复
select a.order,a.color,sum(qty),group_concat(distinct b.po SEPARATOR ',')
from a left join b
on a.order=b.order and a.color=b.color
group by a.order,a.color
cenb03 2008-06-19
  • 打赏
  • 举报
回复
a:

¦order ¦color ¦sizx ¦qty ¦
¦is-01 ¦ red ¦ s ¦ 5 ¦
¦is-01 ¦ red ¦ m ¦ 10 ¦
¦is-01 ¦ red ¦ l ¦ 15 ¦
¦is-01 ¦ blue ¦ s ¦ 5 ¦
¦is-01 ¦ blue ¦ m ¦ 10 ¦
¦is-01 ¦ blue ¦ l ¦ 15 ¦

b:

¦ po ¦order ¦color ¦sizx ¦qty ¦
¦po-01¦is-01 ¦ red ¦ s ¦ 5 ¦
¦po-01¦is-01 ¦ red ¦ l ¦ 15 ¦
¦po-01¦is-01 ¦ blue ¦ s ¦ 5 ¦
¦po-02¦is-01 ¦ red ¦ m ¦ 10 ¦
¦po-02¦is-01 ¦ blue ¦ m ¦ 10 ¦
¦po-02¦is-01 ¦ blue ¦ l ¦ 15 ¦
想要查询结果如下:
¦order ¦color ¦qty ¦ po ¦
¦is-01 ¦ red ¦ 30 ¦ po-01,po-02 ¦
¦is-01 ¦ bule ¦ 30 ¦ po-01,po-02 ¦
懒得去死 2008-06-19
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 dsk22657 的回复:]
5.1后可以用SUM([DISTINCT] expr)
[/Quote]


This keyword has been supported within mysql all the time.
dsk22657 2008-06-19
  • 打赏
  • 举报
回复
5.1后可以用SUM([DISTINCT] expr)
jzyshanxi 2008-06-19
  • 打赏
  • 举报
回复
学习中!!!给点分哦!
wwwwb 2008-06-19
  • 打赏
  • 举报
回复
你只有用SQL+函数来解决,可以参考SQL SERVER这方面的示例
cenb03 2008-06-19
  • 打赏
  • 举报
回复
谢谢楼上帮忙,
但我使用的那个MYSQL版本是4.0,而group_concat要4.1才支持,
请问还有其他办法吗?无法升级MYSQL,因为数据库在别人的服务器上面

56,679

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧