简体   繁体   English

使用SQL JOIN和COUNT

[英]Using SQL JOIN and COUNT

Let there be two tables, one holding user information and one holding user records of some sort, say receipts. 让两个表,一个持有用户信息,一个持有某种用户记录,比如收据。 There is a one-to-many relationship between the users and receipts. 用户和收据之间存在一对多关系。

What would be the best SQL method of retrieving users, sorted by the greatest number of receipts? 检索用户的最佳SQL方法是什么,按最大收据数排序?

The best way I can think of is using a join and count(?) to return an array of users and their number of associated receipts. 我能想到的最好的方法是使用连接和计数(?)来返回用户数组及其关联收据的数量。

Is there a way to make use of the count function in this instance? 有没有办法在这个实例中使用count函数?

select * from `users` inner join `receipts` on `users`.`id` = `receipts`.`uId`

If OP wishes to include additional information (additional aggregations, etc...) utilizing data from users table: 如果OP希望利用来自users表的数据包含其他信息(其他聚合等等):

SELECT `users`.`id`,
       count(`receipts`.`uId`)
FROM `users`
INNER JOIN `receipts` ON `users`.`id` = `receipts`.`uId`
GROUP BY `users`.`id`
ORDER BY count(`receipts`.`uId`) DESC

Otherwise, only the receipts table is required... 否则,只需要receipts表...

SELECT `users`.`id`,
       count(`receipts`.`uId`)
FROM `receipts`
GROUP BY `receipts`.`uId`
ORDER BY count(`receipts`.`uId`) DESC

try this 尝试这个

SELECT a.`id`, count(b.`recipts`) as total_receipts
FROM `users` a
INNER JOIN `receipts` b
ON a.`id` = b.`uId`
GROUP BY a.`id` 
ORDER BY count(b.`receipts`) desc

SELECT users.*, (SELECT COUNT(*) FROM tblreceipts WHERE tblreciepts.uId=users.id) as counter FROM users ORDER BY counter DESC SELECT users.*, (SELECT COUNT(*) FROM tblreceipts WHERE tblreciepts.uId=users.id) as counter FROM users ORDER BY counter DESC

Something like this may work (not sure on the speed though if its big tables) 像这样的东西可能会工作(虽然它的大表不确定速度)

If you want to include all users, even those with no receipts, then a good way is a left outer join : 如果你想包括所有用户,即使那些没有收据的用户,那么一个好的方法是left outer join

SELECT u.*, count(r.uid) as NumReceipts
FROM `users` u left outer join
     `receipts` r
    ON u.id = r.`uId
GROUP BY `u.id
ORDER BY NumReceipts DESC;

If you only want the id for users that have receipts, then the join is not even necessary: 如果您只想拥有收据的用户的ID,则甚至不需要加入:

SELECT r.uid, count(*) as NumReceipts
FROM receipts r
GROUP BY r.uid
ORDER BY NumReceipts

Two answers provided by Dave and meewoK will accomplish what you need. Dave和meewoK提供的两个答案将满足您的需求。 I'm providing an alternative, which should provide better performance and allow you to show more user information because in the case with Dave's answer you can only SELECT columns that are used by an aggregate function or in the group clause. 我提供了一个替代方案,它应该提供更好的性能并允许您显示更多用户信息,因为在Dave的答案中,您只能选择聚合函数或group子句使用的列。

SELECT users.id, users.name, r.numReceipts  
FROM users u
INNER JOIN (
   SELECT uId, count(receipts) as numReceipts
   FROM receipts
   GROUP BY receipts.id
) as r ON r.uId = u.id
ORDER BY r.numReceipts DESC

This creates an inline view. 这会创建一个内联视图。 Only return the count of receipts of each user and then join this inline view on the user's ID. 仅返回每个用户的收据计数,然后将此内联视图加入用户的ID。

Some one correct me if I'm wrong, but I've been told that the planner isn't as efficient when you do a scalar subquery in the SELECT clause. 如果我错了,有人会纠正我,但是我被告知当你在SELECT子句中执行标量子查询时,规划器效率不高。 It's better to join on a temporary table this way. 最好以这种方式加入临时表。 There are multiple ways to write this query and it all depends on how you want to use the information!!! 有多种方法可以编写此查询,这一切都取决于您希望如何使用这些信息! Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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