laravel query builder for mysql中序列的重要性

k97glaaz  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(279)

已编译的查询与代码的顺序不同。查询工作,但我不知道为什么它的工作!
这是生意:
我明白了 roomId$userId ,然后我得到所有 playerIdroomId 步骤:
在表中 room_player_relations ,我吵架了 playerId = $userId .
在表中 room_player_relations ,我得到所有的行在哪里 roomId = roomId 在点1中找到的行的。
选择 playerId 从第2点找到的行。
代码如下:

$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();

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

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'

它看起来很愚蠢,因为它将两个相同的表连接在一起 where 放在后面。但它是有效的。请告诉我为什么?谢谢!

r6vfmomb

r6vfmomb1#

我发现了 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

相关问题