2016-08-03 7 views
0

Ich habe meine Tabelle ‚Rechnungen‘ wie etwas unterWählen Zählung in mysql-Gruppe durch und wiederholen Sie die Zählung in jeder Zeile

Bill   item   totalprice 

BILL_1  Fossil Watch 9000 
BILL_1  Fastrack  - 
BILL_1  Fastrack  - 
BILL_2  Woodlands  7000 
BILL_2  Woodlands  - 
BILL_3  Denim Shirt 9000 
BILL_3  Levis Jean - 

Jetzt muß ich das Ergebnis, wie die unten gezeigte Struktur

Bill   item   totalprice bill_count 

BILL_1  Fossil Watch 9000   3 
BILL_1  Fastrack  -   3 
BILL_1  Fastrack  -   3 
BILL_2  Woodlands  7000   2 
BILL_2  Woodlands  -   2 
BILL_3  Denim Shirt 9000   2 
BILL_3  Levis Jean -   2 
genannt

ich mit der unter Abfrage versucht,

SELECT bill,item,totalPrice,COUNT(bill) FROM bills GROUP BY bill;

aber ich habe das Ergebnis nicht wie erwartet erhalten.

+0

Grund für downvote bitte? –

Antwort

4
SELECT b.bill, b.item, b.totalPrice, tmp.cnt 
FROM bills b 
JOIN 
(
    select bill, COUNT(bill) as cnt 
    from bills 
    group by bill 
) tmp on tmp.bill = b.bill 
+0

Danke für die schnelle Antwort :) –

3
select bill,item,totalPrice,(SELECT 
COUNT(b1.bill) 
FROM 
bills AS b1 where b1.bill= b.bill 
GROUP BY bill) AS bill_count from bills AS b