代码示例如下:
DB::table('orders')->select([
DB::raw('SELECT COUNT(id) AS order_count WHERE type = 2'),
DB::raw('SELECT SUM(id) AS order_sum WHERE type = 2')
])->paginate(20);
选中的字段是统计字段,子查询包含几个条件。我输入这样的代码,但代码无法工作并产生语法错误,那么我无法找出错误的位置。谁可以帮我?谢谢!
答案 0 :(得分:0)
它应该是这样吗?
DB::table('orders')->select(
DB::raw('COUNT(id) AS order_count'),
DB::raw('SUM(id) AS order_sum')
)->where(type, 2)
->paginate(20);
答案 1 :(得分:0)
我的原始SQL就是这样:
SELECT
o1.store_id,
(SELECT Count(id) FROM `order` WHERE type = 2 AND `order`.store_id = o1.store_id) AS package_order_count,
(SELECT SUM(price) FROM `order` WHERE type = 2 AND `order`.store_id = o1.store_id) AS package_order_sum,
(SELECT Count(id) FROM `order` WHERE type = 3 AND `order`.store_id = o1.store_id) AS member_order_count,
(SELECT SUM(price) FROM `order` WHERE type = 3 AND `order`.store_id = o1.store_id) AS member_order_sum
FROM
`order` as o1
WHERE
`o1`.store_id > 0 AND `o1`.`from` = 1
GROUP BY `o1`.store_id
和我的PHP代码一样:
DB::table('orders')->select([
DB::raw('SELECT COUNT(id) AS order_count WHERE type = 2'),
DB::raw('SELECT SUM(id) AS order_sum WHERE type = 2')
DB::raw('SELECT COUNT(id) AS member_order_count WHERE type = 3'),
DB::raw('SELECT SUM(id) AS member_order_sum WHERE type = 3')
])->paginate(20);
所以,我的代码出错了。