php MySql QUERY朋友关系表帮助
编辑:lawrence。php MySql QUERY朋友关系表帮助
我从用户,朋友 得到了正确的查询现在
SELECT * 其中(users.id = friends.user_id1和friends.user_id2 = $配置文件ID)或(users.id = friends.user_id2和friends.user_id1 = $配置文件ID)
问题回答
我需要一些帮助加入我的朋友的结果和用户表
这是我的朋友表是什么样子
id user_id1 user_id2
1 | 2 | 3
1 | 2 | 4
1 | 2 | 5
1 | 6 | 2
用户表
id name
2 | sarah
3 | emma
4 | lawrence
5 | cynthia
6 | suzie
我可以很容易地只是有两行对每个关系,并做了简单的查询。
但我更喜欢有关系的每一个行,
所以让我们假设,我们在看网页member.php?轮廓= 2
并有好友列表,什么是查询样子。
这工作得很好,如果我有关系,每两排,但我不希望出现这种情况....
SELECT * FROM friends, users WHERE friends.user_id1 = $profileID AND friends.user_id2 = users.id ORDER BY friends.id DESC LIMIT 16
你听到了吗?沿东西像
SELECT * FROM friends,users WHERE friends.user_id1 = $profileID AND ALSO WHERE friends.user_id2 = $profileID AND THEN GET FROM users WHERE users.id = friends.user_id1 AND ALSO WHERE users.id = friends.user_id2
我希望我自己清楚
我不知道我理解你的问题,但不会做这?
SELECT * FROM friends, users where friends.user_id1 = $profileID or friends.userid2 = $profileID and users.id = friends.user_id1 or users.id = friends.user_id2
我以前试过,你会认为它可以工作,但我的结果5个结果,而不是4个结果,就像你看我的朋友表一样 – lawrence 2010-09-06 15:16:00
你想要一个left join
(使用LEFT JOIN操作),而不是一个cartesian join
(使用FROM table1, table2
语法)。
http://www.w3schools.com/sql/sql_join_left.asp
提示:有了您的交叉引用表,而不是有一个ID列,您可以创建一个复合键。
为什么朋友id全部设置为1? – RobertPitt 2010-09-06 15:07:01
他们不是正确的查询,但对任何明白我想完成的人,也许可以帮助我,我有搜索组查询和喜欢 – lawrence 2010-09-06 15:09:29