订单表根据商品ID分类,同时根据商品状态获取不同的值

订单表根据商品ID分类,同时根据商品状态获取不同的值_第1张图片 

订单表根据商品ID分类,同时根据商品状态获取不同的值_第2张图片

mysql语句:

1

SELECT o.*,g.name,count(o.status>1 or null) as goods_count_ed,count(o.status=1 or null) as goods_count_ing,sum(o.price) as total_price FROM `test_order` `o` LEFT JOIN `test_goods` `g` ON `g`.`id`=`o`.`goods_id` GROUP BY `o`.`goods_id

查询打印结果:

订单表根据商品ID分类,同时根据商品状态获取不同的值_第3张图片

tp5mysql链式语句:

1
2
3
4
5
6
7

$data = Db::name('order')
->alias('o')
->join('goods g','g.id=o.goods_id','LEFT')
->field('o.*,g.name,count(o.status>1 or null) as goods_count_ed,count(o.status=1 or null) as goods_count_ing,sum(o.price) as total_price ')
->group('o.goods_id')
->select();
halt($data);

原文链接:订单表根据商品ID分类,同时根据商品状态获取不同的值

个人博客:www.liusongs.com

 

你可能感兴趣的:(mysql,ThinkPHP5)