例如有两个表
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版本不支持多重查询

解决方案 »

  1.   

    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 ¦ 
      

  2.   

    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
      

  3.   

    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
      

  4.   

    谢谢楼上帮忙,
    但我使用的那个MYSQL版本是4.0,而group_concat要4.1才支持,
    请问还有其他办法吗?无法升级MYSQL,因为数据库在别人的服务器上面
      

  5.   

    你只有用SQL+函数来解决,可以参考SQL SERVER这方面的示例
      

  6.   

    5.1后可以用SUM([DISTINCT] expr) 
      

  7.   

    This keyword has been supported within mysql all the time.