php - MySql 使用 Max on Date 进行老化报告并排除特定条件

标签 php mysql

我需要为 future 几个月完成的项目准备一份预测老化报告。数据位于各个表中 - 所有表都有一个共同的 post_id 字段。让我们先看看我的表结构(我只会提及相关列)。

  • 表1:01c_posts(ID,post_title,……)
  • 表2:01c_project(id,post_id,end_date,......)
  • 表3:01c_enquairy_products(id,post_id,confirmed_amount,......)
  • 表4:01c_update(id,post_id,status_id,status_date,......)
  • 表5:01c_status(status_id,状态,......)

01c_update 每个 `post_id 都有多个状态。

现在,我准备了以下 SQL,它没有错误,但根据 更新表 中的状态编号为每个项目提供多行 - 所以 sum table3 中的 confirmed_amount 将是相关项目状态数量的倍数。

    SET SQL_BIG_SELECTS=1;
    select a.post_id, b.post_title, 
    sum(if(month(a.end_date) = 1 and year(a.end_date) = 2018,c.confirmed_price, 0)) as "Jan", 
    sum(if(month(a.end_date) = 2 and year(a.end_date) = 2018,c.confirmed_price, 0)) as "Feb", 
    sum(if(month(a.end_date) = 3 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Mar", 
    sum(if(month(a.end_date) = 4 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Apr", 
    sum(if(month(a.end_date) = 5 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "May", 
    sum(if(month(a.end_date) = 6 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Jun", 
    sum(if(month(a.end_date) = 7 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Jul", 
    sum(if(month(a.end_date) = 8 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Aug",

    max(d.update_date), d.status_id
    from 01c_projects a 
    inner join 01c_posts b on a.post_id = b.ID 
    inner join 01c_enquairy_product c on a.post_id = c.post_id 
    inner join 01c_updates d on a.post_id = d.post_id 
    where a.post_id not in (select post_id from 01c_updates where status_id = 25 or status_id = 24)
    group by b.post_title
    having max(d.update_date)
    order by a.end_date

有人可以引导我去哪里吗?

最佳答案

好的,已经解决了。下面给出了最终解决方案,以便其他人在需要时可以获得帮助

    select a.post_id, b.post_title, 
sum(if(month(a.end_date) = 1 and year(a.end_date) = 2018,c.confirmed_price, 0)) as "Jan", 
sum(if(month(a.end_date) = 2 and year(a.end_date) = 2018,c.confirmed_price, 0)) as "Feb", 
sum(if(month(a.end_date) = 3 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Mar", 
sum(if(month(a.end_date) = 4 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Apr", 
sum(if(month(a.end_date) = 5 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "May", 
sum(if(month(a.end_date) = 6 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Jun", 
sum(if(month(a.end_date) = 7 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Jul", 
sum(if(month(a.end_date) = 8 and year(a.end_date) = 2018, c.confirmed_price, 0)) as "Aug",

d.update_date, d.status_id
from 01c_projects a 
inner join 01c_posts b on a.post_id = b.ID 
inner join 01c_enquairy_product c on a.post_id = c.post_id 
inner join 01c_updates d on a.post_id = d.post_id 
where a.post_id not in (select post_id from 01c_updates where status_id = 25 or status_id = 24)
AND d.id in 
(select id from 01c_updates filter join (select post_id, max(update_date) as update_date from 01c_updates
    group by post_id ) sfilter on 
    filter.post_id = sfilter.post_id
    and filter.update_date = sfilter.update_date)
group by b.post_title
order by a.end_date

关于php - MySql 使用 Max on Date 进行老化报告并排除特定条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48640556/

相关文章:

php - 在 Mysql 和 php 中不显示空数据的链接

php - 使用 PHP 生成随机十六进制颜色代码

mysql - 选择具有相同商品名称的行并显示最低价格值

mysql - MariaDB:唯一索引的varchar列大小限制问题

php - 无法使用 PHP 执行多个 MariaDB 查询

php - 按升序显示 wordpress 帖子

php - 为多个数组值创建单个插入语句

mysql - 错误: SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: )

php - 更新我的数据库在 php 中不起作用

php - FOSUserBundle - SQLSTATE[28000] [1045] 用户 'root' @'localhost' 的访问被拒绝(使用密码 : NO))