2表连接和1组连接

[英]2 table join and 1 group concat


I have tables as below

我有如下表格

sales

salesid    |custid    |date_created    |total_price
S0001       C0001      2015-12-31       20.00
S0002       C0002      2016-01-01       10.00
S0003       C0003      2016-01-02       30.00
S0004       C0001      2016-01-05       50.00

salesdetails

salesdetailsid    |salesid    |product_name           |quantity
D0001              S0001       pencil                  3
D0002              S0001       book                    2
D0003              S0001       ruler                   1
D0004              S0002       Hard Cover File Folder  1
D0005              S0003       A4 Paper                1
D0006              S0003       Notebook                1
D0006              S0004       Mouse                   1

My expected output should be:

我的预期输出应该是:

if custid is C0001, output:

如果custid是C0001,输出:

salesid    | Purchase Date    | Items
S0001        2015-12-31         pencil x 3,book x 2, ruler x 1
S0004        2016-01-05         Mouse x 1

if custid is C0002, output:

如果custid是C0002,则输出:

salesid    | Purchase Date    | Items
S0002        2016-01-01         Hard Cover File Folder x 1

if custid is C0003, output:

如果custid是C0003,则输出:

salesid    | Purchase Date    | Items
S0003        2016-01-02         A4 Paper x 1, Notebook x 1

This is what I have so far:

这是我到目前为止:

SELECT s.salesid          AS "id", 
       s.time_created     AS "Purchase Date",   
       s.total_price      AS "Amount", 
       GROUP_CONCAT(Concat(prodname, ' x ', quantity) SEPARATOR ', ') AS "Items" 
FROM   sales s 
JOIN   salesdetails d 
  ON   s.salesid = d.salesid 
WHERE  s.custid = 'C0001' 

1 个解决方案

#1


1  

You are missing your GROUP BY clause, which in effect makes your group_concat work, see if this works:

您缺少GROUP BY子句,这实际上使您的group_concat工作,看看是否有效:

SELECT s.salesid          AS "id", 
       s.time_created     AS "Purchase Date",   
       s.total_price      AS "Amount", 
       Group_concat(Concat(prodname, ' x ', quantity) SEPARATOR ', ') AS "Items" 
FROM   sales s 
JOIN   salesdetails d 
ON     s.salesid = d.salesid 
WHERE  s.custid = 'C0001' 
GROUP BY s.salesid
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2016/05/24/7201a188a482d9ec42567b5446264d1d.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告