【问题标题】:Custom Queries in Laravel [closed]Laravel 中的自定义查询 [关闭]
【发布时间】:2018-01-11 01:01:01
【问题描述】:

如何为多个表编写查询,例如

"select  items.id, items.name, sum(qty) as qty  from dispatch_main 
    inner join dispatch_detail on dispatch_main.id = dispatch_detail.id
    inner join items on items.id = dispatch_detail.item_id
    left outer join customers on dispatch_detail.customer = customers.id
    where dispatch_main.entry_type in ('Purchase','Return','Confirmed') and 
    dispatch_main.to_=$location and items.for_customer in ($types)
    group by dispatch_detail.item_id
    order by  items.id
    ";

"select items.id, items.name, sum(qty) as qty  from dispatch_main 
    inner join dispatch_detail on dispatch_main.id = dispatch_detail.id
    inner join items on items.id = dispatch_detail.item_id
    left outer join customers on dispatch_detail.customer = customers.id
    where dispatch_main.entry_type in ('Dispatch','Confirmed') and 
    dispatch_main.from_=$location and items.for_customer in ($types)
    group by dispatch_detail.item_id
    order by  items.id
    "

在 laravel 5.4 中? DB::statement 可以运行这种类型的查询吗?如果我在 DB::statement(''); 中编写相同类型的查询

【问题讨论】:

  • 你为什么不利用模型和关系并使用Eloquent 来做这些查询?
  • SELECT items.id, items.name .... GROUP BY dispatch_detail.item_id 在较新的 MySQL 服务器上是无效的 SQL。请阅读此内容。 psce.com/en/blog/2012/05/15/…
  • 我不想在这里使用 Eloquent 我只想像在简单的 PHP 中那样运行原始查询。

标签: php mysql laravel laravel-5 laravel-query-builder


【解决方案1】:

你应该试试这个:

DB::table('dispatch_main')
   ->select('items.id',DB::raw('SUM(dispatch_main.qty) as qty'))
   ->join('dispatch_detail', 'dispatch_main.id', '=', 'dispatch_detail.id')
   ->join('items', 'dispatch_detail.item_id', '=', 'items.id')
   ->join('customers', 'dispatch_detail.customer', '=', 'customers.id')
   ->whereIn('dispatch_main.entry_type', ['Purchase','Return','Confirmed'])
   ->where('dispatch_main.to_', $location)
   ->whereIn('items.for_customer', $types)
   ->groupBy('dispatch_detail.item_id')
   ->orderBy('items.id')
   ->get()
   ->toArray();

希望这对你有用!!!

【讨论】:

    【解决方案2】:
      1 DB::table('dispatch_main')
      2   ->innerJoin('dispatch_detail', 'dispatch_main.id', '=', 'dispatch_detail.id')
      3   ->innerJoin('items', 'dispatch_detail.item_id', '=', 'items.id')
      4   ->leftJoin('customers', 'dispatch_detail.customer', '=', 'customers.id')
      5   ->whereIn('dispatch_main.entry_type', ['Purchase','Return','Confirmed'])
      6   ->where('dispatch_main.to_', $location)
      7   ->whereIn('items.for_customer', $types)
      8   ->groupBy('dispatch_detail.item_id')
      9   ->orderBy('items.id')
     10   ->get()->toArray();
     11
     12
    

    试试这个,永远,永远避免写 RAW 查询,直到你绝对有。

    【讨论】:

    • @ParveenKumar 但是这次我想写原始查询,你能告诉我我该怎么做。我不想使用内部连接等 Laravel 函数
    • 这也很简单,你只需要调用你发布的查询类型的静态方法。就像对上述问题的查询将是
    • DB::select("select items.id, items.name, sum(qty) as qty from dispatch_main inner join dispatch_detail on dispatch_main.id = dispatch_detail.id inner join items on items.id = dispatch_detail.item_id 在 dispatch_detail.customer = customers.id 上离开外部加入客户,其中 dispatch_main.entry_type 在('Purchase','Return','Confirmed')和 dispatch_main.to_=? 和 items.for_customer 在(?)分组由 dispatch_detail .item_id order by items.id", [$location, $types]);
    猜你喜欢
    • 2018-06-12
    • 2018-09-16
    • 1970-01-01
    • 2022-12-29
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    相关资源
    最近更新 更多