编译后的查询与代码的顺序不同。 该查询有效,但我不知道为什么有效!
这是公司:
我得到roomId
的{{1}},然后我得到$userId
的所有playerId
步骤:
roomId
中,我得到一行,其中room_player_relations
= playerId
。$userId
中,获得所有行,其中room_player_relations
=在点1中找到的行的roomId
。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'
。但这有效。请告诉我为什么?谢谢!
答案 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