如何將此表加入自己以檢查兩個用戶是否都接受了好友請求?加入自己的表
mysql> select * from friendships;
+---------+-----------+----------+
| user_id | friend_id | accepted |
+---------+-----------+----------+
| 1 | 2 | 1 |
| 2 | 1 | 0 |
| 3 | 1 | 1 |
| 1 | 3 | 1 |
| 1 | 4 | 1 |
| 4 | 1 | 0 |
| 5 | 1 | 1 |
| 1 | 5 | 0 |
+---------+-----------+----------+
8 rows in set (0.00 sec)
並拉出用戶對象。
我可以檢查user 1
是否有任何優秀的朋友請求;
mysql> select * from friendships join users on friendships.friend_id = users.id where friendships.user_id = 1 and accepted = false;
+---------+-----------+----------+----+------------+----------+--------------------+------------------+-----------------+
| user_id | friend_id | accepted | id | fullName | username | phone | verificationCode | accountVerified |
+---------+-----------+----------+----+------------+----------+--------------------+------------------+-----------------+
| 1 | 5 | 0 | 5 | Tom Tester | tom | +16502222222222222 | 4444 | 1 |
+---------+-----------+----------+----+------------+----------+--------------------+------------------+-----------------+
1 row in set (0.00 sec)
但我怎麼讓他接受了請求(即user 1
和user 3
都已經接受了該請求)?
另外我有一個用戶表設置在後臺。
編輯:如果它可以幫助
CREATE TABLE friendships (
user_id int,
friend_id int,
accepted boolean not null default false,
UNIQUE KEY friend (user_id, friend_id)
);
你需要爲你的餐桌,使主鍵連接工作按預期這是在你的餐桌 –
AFAIK失蹤這是友誼表的標準模式。 – ARMATAV
如果您使用表的別名,您可以自行加入。就像這樣:SELECT * FROM friendships JOIN友誼AS反向開啓reverse.friend_id = friendships.user_id –