表联接没有像mysql中所期望的那样工作

x8goxv8g  于 2021-06-17  发布在  Mysql
关注(0)|答案(3)|浏览(265)

我有两张table,第一张是 users_table 第二个是 friends_table . users_table 包含有关用户的信息,包括 banned 列和 friends_table 是用户之间的友谊。
两个表都有列 user_id 所以我想要的是根据两个表中的条件检索特定用户的好友数。条件是开放的 user_id 以及 banned . 我希望朋友的数量排除被禁止的用户,但我仍然得到的朋友总数,即使一个用户的朋友被禁止。请帮助我,以下是我同时运行的检索朋友的查询:

$query1 = "
    SELECT * 
    FROM users_table u 
    INNER JOIN friends_table f ON u.user_id = f.user_id 
    WHERE f.user_id = $user_id AND u.banned = 0
";

$query2 = "
    SELECT * 
    FROM users_table u 
    INNER JOIN friends_table f ON u.user_id = f.friend_id
    WHERE f.friend_id = $user_id AND u.banned = 0
";

编辑“用户表”结构:

user_id
first_name
last_name
username
email
password
temporarily_banned
banned

“朋友表”结构:

friendship_id
user_id
friend_id
friendship_timestamp

表记录示例
用户表示例:

user_id: 20
first_name: Firas
last_name: Helou
username: firashelou
email: mac_987@hotmail.com
password:*******
temporarily_banned: 0
banned: 0

user_id: 30
first_name: Elie
last_name: Helou
username: eliehelou
email: elie@hotmail.com
password:*******
temporarily_banned: 0
banned: 1

user_id: 22
first_name: Jessy
last_name: Helou
username: jessyhelou
email: jessy@hotmail.com
password:*******
temporarily_banned: 0
banned: 1

user_id: 32
first_name: Jad
last_name: Helou
username: jadhelou
email: jad@hotmail.com
password:*******
temporarily_banned: 0
banned: 0

表格示例:

friendship_id | user_id | friend_id | friendship_timestamp
10                20         30             1534342490
9                 20         22             1533484062
16                32         20             1541619611
mm9b1k5b

mm9b1k5b1#

你需要一个额外的 JOINuser 检查朋友是否被禁。
您的第一个查询变成:

SELECT * 
FROM users_table u 
INNER JOIN friends_table f ON f.user_id = u.user_id
INNER JOIN users_table uf ON uf.user_id = f.friend_id AND uf.banned = 0
WHERE u.user_id = $user_id

对于第二个问题:

SELECT * 
FROM users_table u 
INNER JOIN friends_table f ON f.friend_id = u.user_id
INNER JOIN users_table uf ON uf.user_id = f.user_id AND uf.banned = 0
WHERE u.user_id = $user_id

也可以将两个查询合并为一个,如:

SELECT * 
FROM users_table u 
LEFT JOIN friends_table f1 ON f1.friend_id = u.user_id
LEFT JOIN users_table uf1 ON uf.user_id = f1.user_id AND uf1.banned = 0
LEFT JOIN friends_table f2 ON f2.user_id = u.user_id
LEFT JOIN users_table uf2 ON uf2.user_id = f2.friend_id AND uf2.banned = 0
WHERE u.user_id = $user_id AND COALESCE(uf1.id, uf2.id) IS NOT NULL

附言:如果你只是在寻找朋友的数量,你可以使用 SELECT COUNT(*) 而不是 SELECT * .

nx7onnlm

nx7onnlm2#

你需要 JOIN users表显示两次,以确定朋友是否被禁止。查询1:

SELECT *
FROM users_table u
JOIN friends_table f ON f.user_id = u.user_id
JOIN users_table u2 ON u2.user_id = f.friend_id
WHERE u.user_id = $user_id AND u2.banned = 0

对于您提供的示例数据,这将给出除用户\ id 32之外的所有好友的预期结果。
问题2:

SELECT *
FROM users_table u
JOIN friends_table f ON f.friend_id = u.user_id
JOIN users_table u2 ON u2.user_id = f.user_id
WHERE u.user_id = $user_id AND u2.banned = 0

对于您提供的示例数据,这会给出一组不同的结果,因为一些被禁止的用户有未被禁止的朋友。
在dbfiddle上演示

bqjvbblv

bqjvbblv3#

您的查询似乎只是检查用户本身是否被禁止。您需要向“朋友”的用户表中添加另一个联接,以检查他们的状态:

***假设:friend\表只是一个没有“禁止”列的链接表

SELECT * 
FROM users_table user 
INNER JOIN friends_table f ON u.user_id = f.user_id
INNER JOIN users_table friend on friend.user_id = f.user_id
WHERE f.user_id = $user_id AND 0 NOT IN (u.banned, friend.banned)

SELECT * 
FROM users_table u 
INNER JOIN friends_table f ON u.user_id = f.friend_id
INNER JOIN users_table friend on friend.user_id = f.user_id
WHERE f.friend_id = $user_id AND 0 NOT IN (u.banned, friend.banned)

相关问题