2016-03-28 11 views
0
id order_id coupon_code coupon_amount 
1 100  abc123 50 
2 101  abc123 50 
3 102  bca412 100 

und meine Frage istmysql get Zeilenwerte mit Gruppe von

SELECT `coupon_code`, `order_id`, 
     SUM(`coupon_amount`) AS `total`, 
     COUNT(`id`) as `totRow` 
FROM `orders` 
WHERE `coupon_code`!='' 
GROUP BY `coupon_code` 

und soll meine Ausgabe seines

coupon code Usage Total Order Id(s) 
abc123   2 100 100, 101 
bca412   1 100 102 

Wie alle Aufträge-IDs durch Komma in einzelner Abfrage getrennt zu bekommen?

Dank

+0

'SELECT GROUP_CONCAT (DISTINCT order_id) als order_ids, ...' –

+0

Ich weiß nicht, über die MySQL, aber in Oracle kann es geschehen mit LISTAGG, wenn es hilft: http://tech.pranavmaniar.in/oracle-aggregate-string-into-csv/ –

Antwort

2

können Sie GROUP_CONCAT verwenden:

SELECT `coupon_code`, 
     GROUP_CONCAT(`order_id`) as order_ids, 
     SUM(`coupon_amount`) AS `total`, 
     COUNT(`id`) as `totRow` 
FROM `orders` 
WHERE `coupon_code`!='' 
GROUP BY `coupon_code` 
+0

Danke, es funktioniert gut – MGM