[英]mysql - join select data from two different fields in table
I have two tables: 我有两个表:
friends table ( UserID
, FriendID
) and 朋友表(
UserID
, FriendID
)和
users table ( UserID
, FirstName
, LastName
). users表(
UserID
, FirstName
, LastName
)。
I'm trying to do one SQL query to join and pull all records that have the UserID
or FriendID
inside friends table equal to the user's ID but pull the FirstName
and LastName
from the other UserID
. 我正在尝试执行一个SQL查询,以联接并提取所有在友谊表中具有
UserID
或FriendID
记录,这些记录等于用户的ID,但从另一个UserID
提取FirstName
和LastName
。
For example 例如
friends table 朋友表
UserID = 1 | FriendID = 2
UserID = 3 | FriendID = 1
user table 用户表
UserID = 1 | FirstName = "Bob" | LastName = "Hope"
UserID = 2 | FirstName = "John" | LastName = "Doe"
UserID = 3 | FirstName = "Bill" | LastName = "Murray"
If I am logged in as Bob( UserID
= 1) trying to pull all of my friends user data ( FirstName
and LastName
) in one query by checking if UserID
1 is either a FriendID
or UserID
inside the friends table. 如果我以Bob(
UserID
= 1)的身份登录,试图通过检查UserID
1是friends表中的FriendID
还是UserID
在一个查询中提取所有我的朋友用户数据( FirstName
和LastName
)。 Then join the data for the opposite field that isn't my ID. 然后将不是我的ID的相反字段的数据加入。
Any ideas? 有任何想法吗?
-- set the id of the logged in user
set @logged_in = 1;
-- select all the fields from the user table
select users.* from users
-- joined the friends table on the `FriendID`
inner join friends on friends.FriendID = users.UserID
-- filtered by `UserID` on friends table matching logged in user
and friends.UserID = @logged_in -- logged in id
-- union-ed with the users table
union select * from users
-- filtered by the `UserID` being the logged in user
where users.UserID = @logged_in -- logged in id
UserID FirstName LastName
2 John Doe
1 Bob Hope
UserID FirstName LastName
2 John Doe
--
-- Table structure for table `friends`
--
CREATE TABLE IF NOT EXISTS `friends` (
`UserID` int(11) NOT NULL,
`FriendID` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `friends`
--
INSERT INTO `friends` (`UserID`, `FriendID`) VALUES
(1, 2),
(3, 1);
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`UserID` int(11) NOT NULL,
`FirstName` varchar(50) NOT NULL,
`LastName` varchar(50) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`UserID`, `FirstName`, `LastName`) VALUES
(1, 'Bob', 'Hope'),
(2, 'John', 'Doe'),
(3, 'Bill', 'Murray');
Try this: 尝试这个:
SELECT *
FROM users u
WHERE userid IN ( SELECT userid FROM friends WHERE friendid = 1
UNION ALL
SELECT friendid FROM firends WHERE userid = 1);
This will give you: 这将为您提供:
| USERID | FIRSTNAME | LASTNAME |
---------------------------------
| 2 | John | Doe |
| 3 | Bill | Murray |
Select b.uid as userid, a.firstname, a.lastname
from user a
Inner join (select friendid as uid from friends where userid=:currentUser
Union select userid as uid from friends where friendid=:currentUser) b
On a phone, so may need syntax tweaks. 在电话上,因此可能需要语法调整。
An optimiser may suggest a different join strategy based on your real data 优化者可能会根据您的真实数据提出不同的加入策略
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.