0
SELECT * FROM friends
WHERE (user1 = $userid OR user2 = $userid) AND accepted = 1

如果我想从表“users”中获取 user2 的信息,JOIN 会是什么样子?

table friends:
  `id` int(11) NOT NULL auto_increment,
  `user1` int(11) NOT NULL,
  `user2` int(11) NOT NULL,
  `date` datetime NOT NULL,
  `accept` tinyint(1) NOT NULL,
  `type` varchar(32) NOT NULL

users:
  `id` mediumint(8) NOT NULL auto_increment,
  `username` varchar(32) NOT NULL,
  `password` varchar(40) NOT NULL
4

3 回答 3

3
SELECT u.*, f.* FROM friends f 
JOIN users u ON u.id=f.user2 
WHERE (f.user1= $userid OR f.user2=$userid) 
AND f.accepted = 1
于 2009-01-11T23:02:25.257 回答
1

类似于以下内容。

SELECT f.*, u1.*, u2.* FROM friends f
INNER JOIN users u1 ON f.user1 = u1.id
INNER JOIN users u2 ON f.user2 = u2.id
WHERE (f.user1 = $userid OR f.user2 = $userid) AND f.accepted = 1
于 2009-01-11T23:03:43.600 回答
1

你说你没有成功尝试(基于马克泰勒的回答?)

$sql = mysql_query(" SELECT u., f. FROM relations f JOIN users u ON u.id=f.user2 WHERE (f.user1=$row[id] OR f.user2=$userid) AND f.accepted = '1' ");

但不应该

'select u., f. from ...' 

'select u.*, f.* from ...'

?

于 2009-01-11T23:29:04.640 回答