php - Mysql根据一些数据创建一个表

标签 php mysql sql

我有一个装满发票的数据库。年底了,我想做一个年度回顾。我希望评论看起来类似于这张照片:

enter image description here

这是我拥有的数据示例...

enter image description here

这是我到目前为止所做的查询。如果有人可以提供帮助,我将不胜感激。

SELECT invID, invDate, craftArr, vesselTotal, craftGT
FROM company, agents, vessels, invoice
WHERE invoice.status =  '0'
AND invoice.craftArr >  '2014-01-01'
AND invoice.craftArr <  '2014-12-31'
AND invoice.invCraft = vessels.craftID
AND vessels.craftComp = company.compID
AND company.compAgent = agents.agentID
ORDER BY craftArr ASC

最佳答案

我会尝试给你一个线索,告诉你如何实现你的目标。

  1. 遍历 GT 的范围:

    1.1。使用此查询收集给定范围内每个月的总计。 查询将为您提供当前行的金额列表。

    注意。您从您的 PHP 脚本中为 GT 范围开始和结束传递了两个变量。

    SELECT MONTH(craftArr) as monthNumber, sum(vesselTotal) as total FROM company, agents, vessels, invoice WHERE invoice.status = '0' AND invoice.craftArr > '2014-01-01' AND invoice.craftArr < '2014-12-31' AND invoice.invCraft = vessels.craftID AND vessels.craftComp = company.compID AND company.compAgent = agents.agentID AND craftGT between ($START_CURRENT RANGE, $END_CURRENT_RANGE) group by MONTH(craftArr) ORDER BY monthNumber ASC

    1.2。将 $total 变量设置为 0。

    1.3。使用一个循环遍历月份和查询结果:

    注意。如果有几个月没有发票,则金额不存在。您需要在迭代时处理它(monthNumber 可用于此目的)。

    1.3.1。在适当的单元格中输出金额,如果本月没有结果则为 0。

    1.3.2。按当前金额增加 $total

    1.4。当前行的输出总计

关于php - Mysql根据一些数据创建一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28137321/

相关文章:

mysql - PHPMyAdmin - 内置编辑查询失败

mysql - 如何插入到基于其他表的表中?

php - 调试 'A non well formed numeric value encountered' 错误

php - 如何在 Wordpress 中制作 API

php - 有没有办法在 Heroku 中使用 PHP WebSocket?,默认情况下似乎只支持其他技术

mysql 在表之间移动行

mysql - 在 SQL 计数查询中包含零?

php - Heredoc字符串php问题

php - 页面加载后的 Ajax 请求

MySQL 1292 : Truncated incorrect DOUBLE value