【问题标题】:sql, join 2 Tablessql,加入2个表
【发布时间】:2014-09-15 21:01:36
【问题描述】:

我有两张桌子

匹配数据: id_team1, id_team2, name_team1, name_team2, group_order_id

团队: team_id,team_name

我试试这个:

select * from matchdata join teams on matchdata.id_team1 = teams.team_id;

但我需要

matchdata.id_team1 = teams.team_id

matchdata.id_team2 = teams.team_id

之后我想要select WHERE group_order_id = $bla 我该怎么做?

【问题讨论】:

  • 您是否考虑过不在比赛数据中存储名称,而只是在需要时从团队中检索它们?

标签: php sql sqlite join


【解决方案1】:

您可以在一张桌子上使用两个JOIN

SELECT * FROM matchdata
JOIN teams t1 ON matchdata.id_team1 = t1.team_id
JOIN teams t2 ON matchdata.id_team1 = t2.team_id
WHERE group_order_id = $bla;

【讨论】:

    【解决方案2】:

    您可以为此使用IN

    SELECT *
    FROM matchdata md 
        JOIN teams t ON t.team_id IN (md.id_team1, md.id_team2)
    WHERE md.group_order_id = $bla
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 2012-10-17
      • 2023-01-13
      • 1970-01-01
      相关资源
      最近更新 更多