0

我有一个看起来像这样的查询(为了清楚起见,我试图去掉不相关的字段/连接):

SET @num = -1;
SELECT 
*,
CAST(DATE_ADD( '2012-04-01', interval @num := @num+1 day)AS DATE) AS date_sequence, 
DAYOFWEEK(DATE_ADD('2012-04-01', interval @num+1 day)) AS day_week
FROM batch AS b1

left join (
select
batch.`startedDate` AS batch_startedDate,
epiRun.`runType` AS epiRun_runType
.... other fields selected.........
from batch
left join `epiRun` epiRun ON epiRun.`batchID`= batch.`keyID`
.......other joins........
WHERE batch.`startedDate` >= '2012-04-01' AND batch.`startedDate` <= '2012-04-18' 
ORDER BY  batch.`startedDate`ASC) 
AS b2 ON cast((b2.`batch_startedDate`) AS DATE)=CAST(DATE_ADD('2012-04-01', interval @num+1
day)AS DATE)

WHERE
(DATE_ADD('2012-04-01', interval @num+1 day) <= '2012-04-18') 

嵌套选择查询在自己运行时按我预期的方式执行。这个查询有几个问题:

- 批处理表中的每个字段都被选中,但由于这是针对 iReport 的,所以问题不大

- 我得到了从 4 月 1 日到 4 月 18 日的日期列表,但如果我一天有多个批次,那么我只会显示一个 - 理想情况下,我希望日期列中有多个相同的条目,每个批次都有一个唯一的条目。重要的是我可以看到没有批次的日子。

我有的表示例:

Date          Batch
01/04/2012    TS01
02/04/2012    TS03
03/04/2012    null

以及我想生成的内容:

Date          Batch
01/04/2012    TS01
01/04/2012    TS02
02/04/2012    TS03
02/04/2012    TS04
03/04/2012    null
4

1 回答 1

0

I personally would create a separate "dates" table and populate it with all the dates from say 1/1/2000 through 12/31/2050 (or whatever date range will cover all potential queries) and then do a left join from that table to the batch and epiRun tables.

I think that this is a much cleaner way to do what you are looking for and will give you exactly the results you desire.

于 2012-08-15T18:53:19.553 回答