如何在mysql脚本中获取此结构

时间:2018-01-25 03:51:47

标签: mysql codeigniter

我的数据库中有这个表,请参阅附件图片

enter image description here

现在我想要的是计算所有or_number并获得与不同sales_branch中相同sales_date的总sales_total 这是我的示例脚本:

    SELECT *,sum(sales_total) as total,group_concat(DISTINCT or_number) as 
    orn FROM `tbl_sales` GROUP by sales_date

输出: enter image description here

这是错误的,因为它不会将sales_total和不同的sales_branch组合计为一个,因为sales_date属于。

任何想法请帮忙。

1 个答案:

答案 0 :(得分:1)

这是我的SQL脚本:

SELECT *,sales_date, sales_branch, COUNT(DISTINCT or_number), SUM(sales_total) AS total_sales FROM tbl_sales GROUP BY sales_date, sales_branch