我有两个简单的表格:
发票:
ID日期...
1 1/1/2011
2 1/2/2011
Invoice_Products:
invoiceId productId数量
1 101 1000
1 200 50
1 310 125
2 101 2000
2 222 1000
我想要一个具有以下结果的选择查询:
invoiceId date sumProducts
1 1/1/2011 101,200,310
2 1/2/2011 101,222
我的问题是如何将invoice_Products表中的select查询结果添加到单个逗号分隔列中。
任何人都可以帮助我吗?
提前感谢...
答案 0 :(得分:1)
您正在寻找GROUP_CONCAT()
功能。像这样:
SELECT Invoice_Products.invoiceId,
Invoices.Date,
GROUP_CONCAT(Invoice_Products.productId) AS productIds
FROM Invoice_Products
JOIN Invoices ON Invoice_Products.invoiceId = Invoices.ID
GROUP BY invoiceId
;
答案 1 :(得分:0)
尝试这个。
SELECT invoiceID,DATE_FORMAT(now(),'%m/%d/%Y') as `DATE`,GROUP_CONCAT(PRODUCTID) as `sumProducts`
FROM invoice_products
GROUP BY invoiceID;