1

我制作了一个好友列表,其中加入了 2 个表、FRIENDS 表和 PLAYERS 表。

朋友桌

 player   target     status
 -----------------------------
 john     eric       invited
 roger    moore      friends
 stan     winston    friends

球员表

 name      pic     
 -------------------
 john     lol.jpg
 stan     skinhead.gif

现在,在玩家页面上,我需要遍历friends表格并找出他们有多少朋友。因此,为此我必须同时检查player字段和target= $name 的字段

然后要获得 pic id,必须将其与players表 JOIN。

到目前为止,这是我的查询,显然不起作用(有点假):

SELECT p.pic, p.name FROM friends f INNER JOIN players p ON (player OR target)=p.name WHERE (f.player='" .$name. "' OR f.target='" .$name. "') AND status='friends'

$name 是您所在的玩家资料的名称。

谁能告诉我如何调整它以使其工作?

4

3 回答 3

1

SELECT p.pic, p.name FROM friends f INNER JOIN players p ON f.name=p.name WHERE (f.player='" .$name. "' OR f.target='" .$name. "') AND status='friends'

Friends = f.player 在您的查询中?

于 2012-08-05T11:19:56.143 回答
1

您需要使用联合在一个查询中获取所有信息。

SELECT * FROM friends JOIN players ON (target = name AND status = friends)
WHERE player = ?
UNION ALL
SELECT * FROM friends JOIN players ON (player = name AND status = friends)
WHERE target = ?
于 2012-08-05T11:26:43.067 回答
1

尝试这个:

SELECT p.pic, 
       p.name 
FROM friends f 
   INNER JOIN players p ON p.name IN (f.player, f.target)
WHERE (f.player='" .$name. "' OR f.target='" .$name. "') 
AND status='friends'

这是一个简写

SELECT p.pic, 
       p.name 
FROM friends f 
   INNER JOIN players p ON p.name = f.player OR p.name = f.target
WHERE (f.player='" .$name. "' OR f.target='" .$name. "') 
AND status='friends'
于 2012-08-05T11:29:51.893 回答