0

我有以下方案:

CREATE TABLE IF NOT EXISTS `connections` (
`username1` varchar(15) NOT NULL,
`username2` varchar(15) NOT NULL,
PRIMARY KEY (`username1`,`username2`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `connections` (`username1`, `username2`) VALUES
('guy', 'maricela'),
('maricela', 'guy'),
('mikha', 'guy');

CREATE TABLE IF NOT EXISTS `users` (
`username` varchar(15) NOT NULL,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,  
PRIMARY KEY (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `users` (`username`, `first_name`, `last_name`) VALUES
('maricela', 'Maricela', 'Ros'),
('mikha', 'Michael', 'Samuel'),
('guy', 'Guy', 'Marquez');

该方案包含 2 个表;一个用于使用信息users名字、姓氏和用户名调用的用户信息。第二张表是连接。列connections.username1是跟随connections.username2者,也是被跟随者。我想输出关注用户的人员列表,maricela并为每个结果检查结果用户是否被用户关注mikha

我使用以下查询,但检查结果是否跟在后面mikha不起作用:

SELECT followed_by_mikha.username AS followed_by_mikha,
   users.first_name,
   users.last_name,       
   users.username      
   FROM connections
   LEFT JOIN users ON users.username = connections.username1
   LEFT JOIN users followed_by_mikha ON (connections.username1 = 'mikha' AND connections.username2 = users.username AND connections.username2 = followed_by_mikha.username)
   WHERE connections.username2 = 'maricela'
   ORDER BY users.username DESC LIMIT 10

谢谢 :)

4

1 回答 1

1
SELECT fbm.username2 AS followed_by_mikha,
   users.first_name,
   users.last_name,       
   users.username      
   FROM connections
   LEFT JOIN users ON users.username = connections.username1

   LEFT JOIN connections fbm ON fbm.username2=connections.username1 and fbm.username1 = 'mikha'

   WHERE connections.username2 = 'maricela'
   ORDER BY users.username DESC LIMIT 10
于 2012-12-23T16:21:02.787 回答