繁体   English   中英

如何从一张表中选择数据并选择计数到另一张表并在MySQL中组合?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM