简体   繁体   English

如何在一个查询中从3个不同的表中获取3个参数 - SQL

[英]How to get 3 parameters from 3 different tables in one query - SQL

I have these tables: 我有这些表:

CREATE TABLE User( 
Username varchar(15) NOT NULL, 
Name varchar(20) DEFAULT '', 
Surname varchar(20) DEFAULT '', 
Email varchar(30) DEFAULT '', 
City varchar(20) DEFAULT '', 
Description varchar(1000) DEFAULT '', 
userID INTEGER NOT NULL, 
Primary key(Username, userID), 
Foreign key(userID) references Followers(userID) 
);"

CREATE TABLE Followers( 
userID INTEGER NOT NULL, 
usernameFollower varchar(15), 
Primary key(userID), 
Foreign key(usernameFollower) references User(Username) 
);"

CREATE TABLE Post( 
Username varchar(15) NOT NULL, 
userID INTEGER NOT NULL, 
Date DATETIME NOT NULL, 
Message VARCHAR(500) NOT NULL, 
Primary key(Username, Date), 
Foreign key(Username) references User(Username) 
);"

I need to get with a query a list with all the users in User, and for each user the date of the latest post they made and the number of followers they have. 我需要查询一个包含User中所有用户的列表,并为每个用户提供他们发布的最新帖子的日期以及他们拥有的关注者数量。

For example, the output have to be like this: 例如,输出必须是这样的:

USERNAME        LATEST POST                 FOLLOWERS
user1           2011/11/11 18:30:11         5
user2           2011/10/05 17:30:00         1
user3           2010/05/11 18:30:11         90
user4           2011/11/11 18:30:11         5

I can't figure how to make it..I presume for the count of the followers I need to use COUNT(), but how can I link it to the latest post of the user? 我不知道如何制作。.我想使用COUNT()需要关注的人数,但是如何将其链接到用户的最新帖子? Is possible in just one query? 只有一个查询可以吗? Or do I need to use views? 还是我需要使用视图?

Thanks in advance, best regards. 在此先感谢,最好的问候。

You need to join across all 3 tables via the userID. 您需要通过userID连接所有3个表。 You can then select the latest post date by grouping by user id. 然后,您可以按用户ID分组来选择最新的发布日期。 Originally, I also joined onto the Followers table, but this has an unintended side-effect, so... 最初,我也加入了Followers表,但这有意想不到的副作用,所以...

Something like: 就像是:

SELECT U.Name AS UserName,
       MAX(P.Date) AS LatestPost,
       (SELECT COUNT(F.userNameFollower) FROM Followers WHERE userID = U.userID) AS FollowerCount
FROM User U
INNER JOIN Post P ON (U.userID = P.userID)

GROUP BY U.UserID

Should work. 应该管用。

Please note that my query is assuming that a user has posted. 请注意,我的查询假设用户已发布。 If they haven't then change the INNER JOIN to a LEFT JOIN. 如果没有,则将INNER JOIN更改为LEFT JOIN。

The reason the original query doesn't work is that it matches a row in Post for each user, and a row in Followers for each user - thus multiplying the number of results. 原始查询不起作用的原因是它匹配每个用户的Post中的行,以及每个用户的追随者中的行 - 从而将结果的数量相乘。 Instead, we can use a subquery to select the count of followers with the matching userid in the User table. 相反,我们可以使用子查询在User表中选择具有匹配用户ID的关注者数量。

HOWEVER ... this only counts the total number of followers a user has, not the number of followers for a post. 但是 ......这只计算用户拥有的关注者总数,而不是帖子关注者的数量。 The reason for this is because you have only linked a follower to a user. 原因是因为您只将关注者链接到用户。 You'd also need to have a relationship between Follower and Post. 你还需要在追随者和帖子之间建立关系。

You can do this in a number of ways; 你可以通过多种方式做到这一点; you can create a PostFollower table consisting of PostId and UserId (so you can see which user has followed each post) or you can add a PostId to the Follower table, and store the follower userId and the PostId, allowing you to store which user followed which posts. 您可以创建一个由PostId和UserId组成的PostFollower表(这样您就可以看到哪个用户关注了每个帖子),也可以将PostId添加到Follower表中,并存储关注者userId和PostId,从而可以存储哪个用户关注了哪个帖子。

It will be something like below.Please do necessary change 如下图所示。请进行必要的更改

select u.Username,max(p.Date) as latest_post,count(f.userId) as totalfollowers 
from users as u 
 left join post as p on u.userId=p.userId 
 left join followers as f on u.userId-f.userId 
 order by p.date desc 
 group by u.userId

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

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