Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql从特定日期选择1周的数据_Mysql_Sql - Fatal编程技术网

Mysql从特定日期选择1周的数据

Mysql从特定日期选择1周的数据,mysql,sql,Mysql,Sql,我有两张表,分别是项目和项目资助 产品表是这样的 +-----------------+ | id | date_created | +-----------------+ | 1 | 2014-09-10 | | 2 | 2014-05-20 | | 3 | 2014-02-20 | | 4 | 2014-02-20 | +-----------------+ +------------------------------+ | id | date_funded | Proj_id

我有两张表,分别是项目和项目资助

产品表是这样的

+-----------------+
| id | date_created |
+-----------------+
| 1  | 2014-09-10 |
| 2  | 2014-05-20 |
| 3  | 2014-02-20 |
| 4  | 2014-02-20 |
+-----------------+
+------------------------------+
| id | date_funded | Proj_id  |
+------------------------------+
| 1  | 2014-09-10 |      1    |
| 2  | 2014-09-11 |      1    |
| 3  | 2014-09-11 |      1    |
| 4  | 2014-09-12 |      1    |
+------------------------------+
我的项目资助表是这样的

+-----------------+
| id | date_created |
+-----------------+
| 1  | 2014-09-10 |
| 2  | 2014-05-20 |
| 3  | 2014-02-20 |
| 4  | 2014-02-20 |
+-----------------+
+------------------------------+
| id | date_funded | Proj_id  |
+------------------------------+
| 1  | 2014-09-10 |      1    |
| 2  | 2014-09-11 |      1    |
| 3  | 2014-09-11 |      1    |
| 4  | 2014-09-12 |      1    |
+------------------------------+
现在,如何从项目1的开始日期获取项目1的下一周数据计数

例如,项目编号1于2014年9月10日启动

我需要这样的结果

2014-09-10 => 1
2014-09-11 => 2 
2014-09-12 => 1

这是一个基本查询,其中包含group by

select
      pf.date_funded,
      count(*) as totalEntries
   from
      project_funded pf
   where
      pf.proj_id = 1
   group by
      pf.date_funded
   order by
      pf.date_funded
现在,如果您只关心项目创建日期后的1周,那么这将需要对项目表进行额外的联接,并需要基于项目的创建日期的日期范围限制

select
      pf.date_funded,
      count(*) as totalEntries
   from
      project_funded pf
         JOIN project p
            on pf.proj_id = p.id
   where
          pf.proj_id = 1
      AND pf.date_funded >= p.date_created
      AND pf.date_funded <  DATE_ADD(p.date_created, INTERVAL 8 DAY);
   group by
      pf.date_funded
   order by
      pf.date_funded

在上面带有日期的示例中,您提到了1周7天,这可能是间隔1周。但是,如果您的日期是基于日期/时间的,那么您真的希望在晚上11:59:59之前(包括晚上11:59)的7天内都有约会。这就是为什么我只做了不到8天的工作,所以它包括了从7天到第8天晚上结束的所有工作。

继续。试试看。正如@草莓所提到的,你能告诉我们你尝试了什么吗?还有一个简单的提示。。。它需要一个GROUP BY子句: