简体   繁体   English

SQL如何连接这些表

[英]SQL how to join these tables

The scenario: 场景:

I have a website which let users vote between cars which they like most. 我有一个网站,让用户在他们最喜欢的汽车之间投票。 Cars are saved in the table cars , votes are saved in votes and the column country_id from the table cars reference to countries (where the carbrand comes from). 汽车被保存在表中cars ,票都保存在votes ,并从表中的列COUNTRY_ID cars参照countries (其中carbrand来自)。

I want to show the users which country has the most votes. 我想向用户显示哪个国家/地区的投票率最高。 Simple version of the tables: 简单版本的表格:

CARS 汽车

id
name
country_id

Countries 国家

id
name

Votes 投票

id
user_id
car_id

Ideally I would like to show the users the top x countries. 理想情况下,我想向用户显示前x个国家/地区。 And how many votes they all have. 他们都有多少票。

Bonus: would it be possible to use this query for a certain user? 额外奖励:是否可以将此查询用于特定用户? So they see their top x with countries they voted on. 所以他们看到他们的最高x与他们投票的国家。

And which indexes you suggest? 你建议哪个指数? The votes table can grow beyond 10 million votes, the cars table can grow fast too. 投票表可以超过1000万票,汽车表也可以快速增长。

I think you can achieve this with a LEFT JOIN query and GROUP BY aggregate function 我认为你可以通过LEFT JOIN查询和GROUP BY聚合函数来实现这一点

SELECT COUNT(a.id) as total_votes, c.name as country_name
FROM Votes a
LEFT JOIN CARS b
ON a.car_id = b.id
LEFT JOIN Countries c
ON b.country_id = c.id
GROUP BY b.name, c.name
ORDER BY total_votes DESC

Indexes on Cars.CountryID, Votes.UserID and Votes.CarID would seem reasonable. Cars.CountryID,Votes.UserID和Votes.CarID上的索引似乎是合理的。 As mzedler suggested though, when you get up to tens of millions, aggregates can be a bad idea. 正如mzedler建议的那样,当你达到数千万时,聚合可能是一个坏主意。

There are number of ways of addressing that, triggers, a cache, or adding date voted to votes, so you break down the number of records you have to count in one go. 有多种方法可以解决这些问题,触发器,缓存或添加投票选票的日期,因此您可以分解一次必须计算的记录数。 eg cache votes daily and then just query those made since midnight and then sum them. 例如,每天缓存投票,然后查询自午夜以来的投票,然后对它们求和。

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

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