Laravel查询构建器中针对MySQL的序列如何起作用

时间:2018-09-20 14:55:47

标签: mysql laravel sequence query-builder

编译后的查询与代码的顺序不同。 该查询有效,但我不知道为什么有效!

这是公司:

我得到roomId的{​​{1}},然后我得到$userId的所有playerId

步骤:

  1. 在表roomId中,我得到一行,其中room_player_relations = playerId
  2. 在表$userId中,获得所有行,其中room_player_relations =在点1中找到的行的roomId
  3. 从第2点中找到的行中选择roomId

代码如下:

playerId

这是在调试栏中看到的查询:

$players = DB::table('room_player_relations as t1')
        ->where('t1.playerId',$userId)
        ->join('room_player_relations as t2','t1.roomId','=','t2.roomId')            
        ->select('t2.playerId')
        ->get();    

它看起来很愚蠢,因为它将2个相同的表连接在一起,之后是select `t2`.`playerId` from `room_player_relations` as `t1` inner join `room_player_relations` as `t2` on `t1`.`roomId` = `t2`.`roomId` where `t1`.`playerId` = '1' 。但这有效。请告诉我为什么?谢谢!

1 个答案:

答案 0 :(得分:0)

我发现INNER JOIN-两个表不是将两个表粘在一起,而是匹配匹配键的所有可能方式(在我的示例中为roomId)。我实际上可以通过以下查询看到INNER JOIN的结果:

select * from `room_player_relations` as `t1` 
inner join `room_player_relations` as `t2` 
on `t1`.`roomId` = `t2`.`roomId`
  

playerId roomId玩家Id roomId

     

2 2 2 2

     

3 2 2 2

     

1 2 2 2

     

2 2 3 2

     

3 2 3 2

     

1 2 3 2

     

4 1 4 1

     

5 1 4 1

     

4 1 5 1

     

5 1 5 1

     

2 2 1 2

     

3 2 1 2

     

1 2 1 2