SQLite连接多个匹配

时间:2017-09-04 13:56:39

标签: sql join

我有3张桌子

PLAY
_id INTEGER
time INTEGER

SCORE
_id INTEGER
player_id INTEGER
play_id INTEGER
civilian INTEGER
scientific INTEGER
commercial INTEGER
military INTEGER
guilds INTEGER
treasury INTEGER
wonders INTEGER
progress INTEGER
total INTEGER
supremacy INTEGER
victory INTEGER

PLAYER
_id INTEGER
first_name INTEGER
last_name INTEGER

每个 PLAY 两个 SCORE 被链接到每个 SCORE 一个 PLAYER 是链接。

我想SELECT每个播放,并且第一个和第二个 SCORE 播放器的名字链接到每个播放即可。我认为使用JOIN语句是可能的,但由于我不是SQL专家,我无法弄清楚如何做到这一点。我的桌子甚至可以吗?

修改
我尝试了Juan Carlos Oropeza的答案,并对SELECT陈述进行了一些修改

SELECT p._id, p.time, p1.first_name, p2.first_name
FROM play p
  JOIN score s1
    ON p._id = s1.play_id
  JOIN score s2
    ON p._id = s2.play_id
       AND s1.player_id <> s2.player_id
  JOIN player p1
    ON s1.player_id = p1._id
  JOIN player p2
    ON s2.player_id = p2._id

我得到了这个结果,_id列包含播放 ID&#39; s

_id time            first_name  last_name
1   1504107269335   Jelmer      Amarinske
1   1504107269335   Amarinske   Jelmer
2   1504529628826   Jelmer      Amarinske
2   1504529628826   Amarinske   Jelmer
3   1504529644821   Jelmer      Amarinske
3   1504529644821   Amarinske   Jelmer

它几近完美。现在我每场比​​赛只想要一排。

编辑:

我添加了Juan Carlos Oropeza的建议。现在我有了预期的结果。

1   1504107269335   Jelmer  Amarinske
2   1504529628826   Jelmer  Amarinske
3   1504529644821   Jelmer  Amarinske

1 个答案:

答案 0 :(得分:0)

您必须在分数表中加入两次。然后与玩家一起加入每个分数以获得名称

public class contracts {
  public String           bez;
  public ArrayList<Years> years;
  ...getter and setter
}


public class years{
  public String                 year;    
  public ArrayList<Insurance> insurance;
  ...getter and setter
}


public class Insurance {    
   public String name;
    ...getter and setter
}

 controller:  model.addAttribute("data", initialization of all classes);   

<div  th:each="year : ${data.years}">
  <div  th:each="vs, stat : ${year.insurance}">
    <div th:text="${vs.name}"></div> -> OK!!!
    <input type="text" th:field="*{vs[__${stat.index}__].name}"></input> -> ERROR!!!
  </div>
</div>