【发布时间】:2013-07-22 19:58:16
【问题描述】:
我有两张桌子
帐户 { acc_id, score,... }
朋友 { acc_id,friend_id }
我需要查询来选择约翰所有朋友的分数(例如)
有点像
SELECT (acc_id,score) FROM accounts WHERE(表友包含条目 (John,acc_id))
这样的查询可以写吗??
谢谢
【问题讨论】:
标签: mysql select conditional-statements
我有两张桌子
帐户 { acc_id, score,... }
朋友 { acc_id,friend_id }
我需要查询来选择约翰所有朋友的分数(例如)
有点像
SELECT (acc_id,score) FROM accounts WHERE(表友包含条目 (John,acc_id))
这样的查询可以写吗??
谢谢
【问题讨论】:
标签: mysql select conditional-statements
是的,您需要使用子查询。它应该看起来像这样:
Select acc_id, score from accounts
where acc_id in (select acc_id where friend_id = 'John'sID');
【讨论】:
使用子查询:
Select acc_id, score from accounts
where acc_id in (select acc_id from friends where friend_id = 'John'sID');
或 使用联接:
Select accounts.acc_id, score from accounts
Left Join friends using (acc_id)
where friend_id = 'John'sID';
【讨论】: