【发布时间】: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