如何从数据库

时间:2017-05-21 09:05:21

标签: php mysql laravel-5

我有一个名为:expensesments(给定图像号码1)的数据库表,用于存储一些官方支出。 enter image description here

说明:此处sub_head_id是与另一个表名相关的外键:存储sub_head名称的子标题(给定图像编号2)。 ex_date存储支出日期。 enter image description here

我想要的是:我希望每月获得这些支出的年度报告。 期望的结果: enter image description here

如何使用laravel 5.4在php.blade文件中制作这个? 有人帮忙吗?

1 个答案:

答案 0 :(得分:1)

您可以使用多个SELECT语句生成数月的列,例如:

SELECT s.head_id, s.acc_sub_head,
 (SELECT SUM(amount) FROM exenditures WHERE sub_head_id = s.head_id AND ex_date BETWEEN '2017-01-01' AND '2017-01-31') AS 'January 2017',
 (SELECT SUM(amount) FROM exenditures WHERE sub_head_id = s.head_id AND ex_date BETWEEN '2017-02-01' AND '2017-02-28') AS 'February 2017',
 (SELECT SUM(amount) FROM exenditures WHERE sub_head_id = s.head_id AND ex_date BETWEEN '2017-03-01' AND '2017-03-31') AS 'March 2017',
 (SELECT SUM(amount) FROM exenditures WHERE sub_head_id = s.head_id AND ex_date BETWEEN '2017-04-01' AND '2017-04-30') AS 'April 2017',
FROM exenditures e JOIN subheads s ON e.sub_head_id = s.head_id
GROUP BY s.head_id, s.acc_sub_head;

您可以在几个月内添加更多SELECT次查询。