【问题标题】:Laravel 4 query builder - with complicated left joinsLaravel 4 查询生成器 - 具有复杂的左连接
【发布时间】:2014-01-13 10:58:57
【问题描述】:

我是 Laravel 4 的新手。

我有这个问题:

SELECT a.id, active, name, email, img_location, IFNULL(b.Total, 0) AS LeadTotal, IFNULL(c.Total, 0) AS InventoryTotal
FROM users AS a
LEFT JOIN (
   SELECT user_id, count(*) as Total
   FROM lead_user
   GROUP BY user_id
) AS b ON a.id = b.user_id
LEFT JOIN (
   SELECT user_id, count(*) as Total
   FROM user_inventory
   GROUP BY user_id
) AS c ON a.id = c.user_id
WHERE a.is_deleted = 0

如何将其转换为 Laravel 查询构建器?我对如何将 Laravel 连接查询构建器与此类查询一起使用感到困惑。

回答!!

请在 laravel 论坛上得到 petkostas 的所有帮助。我们得到了答案。

$users = DB::table('users AS a')
->select(array('a.*', DB::raw('IFNULL(b.Total, 0) AS LeadTotal'), DB::raw('IFNULL(c.Total, 0) AS InventoryTotal')  ) )
->leftJoin(DB::raw('(SELECT user_id, COUNT(*) as Total FROM lead_user GROUP BY user_id) AS b'), function( $query ){
    $query->on( 'a.id', '=', 'b.user_id' );
})
->leftJoin(DB::raw('(SELECT user_id, COUNT(*) as Total FROM user_inventory WHERE is_deleted = 0 GROUP BY user_id) AS c'), function( $query ){
    $query->on( 'a.id', '=', 'c.user_id' );
})
->where('a.is_deleted', '=', 0)
->get();

【问题讨论】:

    标签: php mysql sql laravel-4 laravel-query-builder


    【解决方案1】:

    我相信,使用 ORM 关系是连接表格的更好方法。

    请参考链接: https://laravel.com/docs/5.7/eloquent-relationships

    【讨论】:

      【解决方案2】:

      这种类型的查询很难用查询构建器构建。但是,您可以使用DB::select

      如果您没有要绑定的内容,您可以使用以下方法:

      DB::select("SELECT a.id, active, name, email, img_location, IFNULL(b.Total, 0) AS LeadTotal, IFNULL(c.Total, 0) AS InventoryTotal
      FROM users AS a
      LEFT JOIN (
         SELECT user_id, count(*) as Total
         FROM lead_user
         GROUP BY user_id
      ) AS b ON a.id = b.user_id
      LEFT JOIN (
         SELECT user_id, count(*) as Total
         FROM user_inventory
         GROUP BY user_id
      ) AS c ON a.id = c.user_id
      WHERE a.is_deleted = 0");
      

      如果需要与查询绑定参数:

      $deleted = 0;
      
      DB::select("SELECT a.id, active, name, email, img_location, IFNULL(b.Total, 0) AS LeadTotal, IFNULL(c.Total, 0) AS InventoryTotal
      FROM users AS a
      LEFT JOIN (
         SELECT user_id, count(*) as Total
         FROM lead_user
         GROUP BY user_id
      ) AS b ON a.id = b.user_id
      LEFT JOIN (
         SELECT user_id, count(*) as Total
         FROM user_inventory
         GROUP BY user_id
      ) AS c ON a.id = c.user_id
      WHERE a.is_deleted = ?", [$deleted]);
      

      【讨论】:

      • 我可以使用它,但我认为有一种方法可以使用查询构建器连接来管理它。不管怎么说,还是要谢谢你。这将是我最后的手段。
      【解决方案3】:

      我相信这应该可行:

      $users = DB::table('users')
          ->select( array('users.*', DB::raw('COUNT(lead_user.user_id) as LeadTotal'), DB::raw('COUNT(user_inventory.user_id) as InventoryTotal') ) )
          ->leftJoin('lead_user', 'users.id', '=', 'lead_user.user_id')
          ->leftJoin('user_inventory', 'users.id', '=', 'user_inventory.user_id')
          ->where('users.is_deleted', '=', 0)
          ->get();
      

      【讨论】:

      • 输出不正确。当它在lead_user 和user_inventory 表之间找到一条记录时,它会加倍计数。
      猜你喜欢
      • 2014-05-13
      • 2022-01-20
      • 2022-01-24
      • 1970-01-01
      • 2019-10-23
      • 1970-01-01
      • 2014-12-06
      • 2016-11-12
      • 1970-01-01
      相关资源
      最近更新 更多