【问题标题】:Complex Query with another query in the condition条件中包含另一个查询的复杂查询
【发布时间】: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


    【解决方案1】:

    是的,您需要使用子查询。它应该看起来像这样:

        Select acc_id, score from accounts 
        where acc_id in (select acc_id where friend_id = 'John'sID');
    

    【讨论】:

      【解决方案2】:

      使用子查询:

      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';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-03
        • 1970-01-01
        • 2017-12-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多