ON子句Laravel ORM中的子查询

时间:2015-09-01 19:26:49

标签: sql laravel orm eloquent laravel-5.1

如何在Laravel ORM中创建此查询?

SELECT b1.* FROM `buildings` b1 INNER JOIN `buildings` b2 ON b2.`id` IN (SELECT `id` FROM `ingame_buildings`) WHERE b1.`level` > b2.`level`

我试过->join()

function(){
    return implode(',',IngameBuilding::get());
}

但它没有用。

1 个答案:

答案 0 :(得分:1)

Here is the query to get all the records in buildings that have level greater than the existing building level in ingame_buildings

SELECT * FROM buildings
JOIN (
      SELECT a.building_id, b.level 
      FROM ingame_buildings as a
      JOIN buildings as b ON a.building_id=b.id
     ) 
AS ingame_level ON ingame_level.building_id=buildings.id
WHERE buildings.level > ingame_level.level