简体   繁体   English

SQL内部连接有3个表

[英]SQL Inner Join with 3 tables

So I believe I need a Inner Join for this query, but am not 100% sure. 所以我认为我需要一个内部联接来进行此查询,但我不是100%肯定。

First of all see my database diagram: 首先看我的数据库图:

Click here for database diagram 单击此处查看数据库图表

What I'm trying to achieve: 我想要实现的目标:

  • I'm trying to get all messages (so user_username, text, posted_at FROM Messages) where the user_username matches with the followed_username, and where the followed_username has a follower_username that = ?. 我正在尝试获取user_username与followed_username匹配的所有消息(所以user_username,text,posted_at FROM Messages),以及follow_username的follower_username =?。
  • So essentially, everyone followed by ?, I want to get all their messages. 基本上,每个人都跟着?,我想得到他们所有的信息。

Where ? 哪里? = an inputed username =一个输入的用户名

What I've tried so far 到目前为止我尝试过的

I've tried a number of sql statements and have thus far been unable to get be successful. 我已经尝试了许多sql语句,但到目前为止还没有成功。 These are some I have tried. 这些是我尝试过的。

$sql = "SELECT user_username, text, posted_at FROM Messages, Users, User_Follows WHERE user_username = (SELECT username FROM Users WHERE username = (SELECT followed_username FROM User_Follows WHERE follower_username = ?)) ORDER BY posted_at DESC;";
$sql = "SELECT user_username, text, posted_at FROM Messages, User_Follows WHERE follower_username = ? AND followed_username = user_username;";
$sql = "SELECT user_username, text, posted_at FROM Messages JOIN User_Follows ON user_username = followed_username WHERE follower_username = followed_username;";

I now think I need to use an inner join to achieve what I want, but am not too sure whether this is correct, or how to go about it. 我现在认为我需要使用内连接来实现我想要的,但我不太确定这是否正确,或者如何去做。

Thanks in advance. 提前致谢。

Try something like: 尝试类似的东西:

SELECT M.user_username, M.text, M.posted_at 
FROM Messages M  
INNER JOIN Users U on  M.user_username= u.username
INNER JOIN User_Follows UF  on UF.followed_username = u.username
WHERE UFfollower_username = ? 
ORDER BY posted_at DESC;

Note for future designs that using a long varchar or nvarchar field is a poor choice for a field you will be joining on. 请注意,对于将要加入的字段,使用long varchar或nvarchar字段的未来设计是一个糟糕的选择。 Further, if the field is a character type field, it is often subject to being changed over time which is also a bad choice for a key field. 此外,如果字段是字符类型字段,则它经常会随着时间而改变,这对于关键字段也是不好的选择。

Integer joins are much faster generally. 整数连接通常要快得多。 It might be ok if it can save you from having to to do some joins but in general it is a poor idea. 它可能没问题,如果它可以让你不必去做一些连接,但总的来说这是一个糟糕的主意。

If someone changed his username (which does happen in every system I have ever worked in) you would then have to update all the child tables which could be quite a lot of records to update. 如果有人改变了他的用户名(这确实发生在我曾经工作过的每个系统中),那么你必须更新所有子表,这些表可能需要更新很多记录。 If you used a surrogate key, you would only need to update the parent table. 如果您使用了代理键,则只需要更新父表。

Additionally the word text is a reserved word for many databases and it is best to avoid those in naming fields. 此外,单词text是许多数据库的保留字,最好避免命名字段中的那些。

a simple sub query should do it: 一个简单的子查询应该这样做:

select * from messages where user_username in
(
  select followed_username from user_follows
  where follower_username = 'your_input_username'
);

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

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