如何从CURDATE()前n天获取MySQL的值

时间:2015-09-19 08:08:07

标签: php mysql

我想加入4个表来列出表中的所有值,这些值从上次更新到当前日期的持续时间多于其他表格中的持续时间,表格如下(我的英语不太好理解,所以我在解释考试)

第一张表daily_tasks

+---------+---------+
| task_id | type_id |
+---------+---------+
|       1 |       1 |
|       2 |       1 |
|       3 |       2 |
+---------+---------+

第二张表daily_task_report

+-----------+---------+------------+
| report_id | task_id | task_date  |
+-----------+---------+------------+
|         1 |       1 | 2015-09-10 |
|         2 |       3 | 2015-09-10 |
|         3 |       1 | 2015-09-11 |
|         4 |       3 | 2015-09-16 |
+-----------+---------+------------+

第三表duration_types

+---------+---------------+------------------------+
| type_id | duration_type | duration_time(in days) |
+---------+---------------+------------------------+
|       1 | Daily Task    |                      1 |
|       2 | Weekly Task   |                      6 |
|       3 | Monthly Task  |                     26 |
|       4 | Yearly Task   |                    313 |
+---------+---------------+------------------------+

第四表calendar

+--------+------------+---------+
| cal_id |  cal_date  | holiday |
+--------+------------+---------+
|      1 | 2015-09-10 |       0 |
|      2 | 2015-09-11 |       0 |
|      3 | 2015-09-12 |       0 |
|      4 | 2015-09-13 |       1 |
+--------+------------+---------+

此处daily_tasks.type_id来自duration_types.type_iddaily_task_report.task_id来自daily_tasks.task_id。我想选择所有task_id task_datecurrent_date差异大于duration_time,同时计算持续时间我必须避免holiday=1的日期来自calendar。 我尝试了查询但不正确,我得到的值没有包含calendar表,但这不是一个好方法,查询需要更多时间来执行。

"SELECT dailyTasks.task_id FROM 
                          (SELECT tab.* FROM (SELECT 
                          tasks.task_type,report.*
                          FROM daily_tasks AS tasks
                          LEFT JOIN daily_task_reports AS report ON tasks.task_id=report.task_id
                          WHERE 1 ORDER BY report.task_date DESC) as tab GROUP BY tab.d_task_id) AS dailyTasks
                          LEFT JOIN duration_types AS type ON dailyTasks.task_type=type.type_id
                          WHERE DATEDIFF(CURDATE(),dailyTasks.task_date)>=type.duration_time"

请有人帮忙,我坚持了这一部分

1 个答案:

答案 0 :(得分:0)

根据给定的表,您的查询中有一些意外的文本或未知列

  

尝试此查询

"SELECT dailyTasks.d_task_id FROM 
                      (SELECT tab.* FROM 
                                (SELECT tasks.type_id,report.* FROM daily_tasks AS tasks

                                    LEFT JOIN daily_task_reports AS report ON tasks.task_id=report.task_id

                                    ORDER BY report.task_date DESC) 

                                as tab GROUP BY tab.task_id) AS dailyTasks

                                LEFT JOIN duration_types AS type ON dailyTasks.type_id=type.type_id

                      WHERE DATEDIFF(CURDATE(),dailyTasks.task_date)>=type.duration_time"
  

它也适用于单个查询

SELECT tasks.task_id FROM daily_tasks AS tasks

        LEFT JOIN daily_task_reports AS report ON tasks.task_id=report.task_id
        LEFT JOIN duration_types AS type ON tasks.type_id = type.type_id and DATEDIFF(CURDATE(),report.task_date) >=  type.duration_time

*我试过但不排除在日历中有假期的id 您可以在编码方面创建它我给了您日历中包含的新查询 *

SELECT tasks.task_id,report.task_date,calendar.holiday FROM daily_tasks AS tasks

        LEFT JOIN daily_task_reports AS report ON tasks.task_id=report.task_id
        LEFT JOIN duration_types AS type ON tasks.type_id = type.type_id and DATEDIFF(CURDATE(),report.task_date) >=  type.duration_time
        LEFT JOIN calendar ON report.task_date=calendar.cal_date 
        where calendar.holiday = '0'
        order By report.task_date desc