[英]MySQL syntax to combine row count in one table with select results from another table
[英]How to select data from one table and select count to another table and combine it in MySQL?
在一個水果中可以有多張票可以籌集。 我需要顯示每個水果籌集的票數。 他們的關鍵字段是Fruit_id 。
如果我有以下表格:
id name
1 apple
2 orange
id fruit_id
1 1
2 1
3 2
4 2
5 2
然后我會使用下面的 SQL 語法來輸出一個你需要的表:
SELECT fruit.id, fruit.name, COUNT(tickets.id)
FROM tickets
LEFT JOIN fruit ON fruit.id = tickets.fruit_id
GROUP BY fruit.id;
輸出:
id name COUNT(tickets.id)
1 apple 2
2 orange 3
SELECT Fruit.Fruit ID,Fruit.Fruit Name, count(Ticket.Ticket Id) as match_rows FROM Fruit LEFT Join Ticket on (Fruit.Fruit ID = Ticket.Fruit ID) group by Fruit.Fruit Id ORDER BY Fruit.Fruit ID DESC
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.