我需要创建一个查询以基于日期间隔返回 N 行。这很简单,但我不能:/
基本查询:
SELECT
f.many_fields
FROM
tb_foo f
WHERE
f.id_foo = 14
此查询根据我的 ID 返回 1 行。但是,我在“2012-10-30”处设置了“2012-10-10”之类的日期间隔来应用于此查询,最后基本查询将根据日期间隔的差异返回 20 行。
可以做到这一点吗?
SELECT
f.many_fields,
bar.*
FROM
tb_foo f,
(SELECT x FROM dual limit DATEDIFF('2012-10-10','2012-10-30')) bar
WHERE
f.id_foo = 14
真实查询:
SELECT
m.id_matricula,
a.nome
FROM
tb_matricula m
inner join tb_aluno a on (a.id_aluno = m.id_matricula)
WHERE
m.id_matricula = 14
开始日期 = 2012-10-10 结束日期 = 2012-10-30
我将在 iReport 上使用此结果,根据此日期之间的差异构建一个包含行数的网格。
基于 Google 搜索的示例:
SELECT
CONCAT( dt.d ,'-' ,days.d ) AS dates
FROM
(
SELECT
CONCAT( a1 ,b1 ) AS d
FROM
(
SELECT
'0' AS a1
UNION ALL SELECT
'1'
UNION ALL SELECT
'2'
UNION ALL SELECT
'3'
) a JOIN(
SELECT
'0' AS b1
UNION ALL SELECT
'1'
UNION ALL SELECT
'2'
UNION ALL SELECT
'3'
UNION ALL SELECT
'4'
UNION ALL SELECT
'5'
UNION ALL SELECT
'6'
UNION ALL SELECT
'7'
UNION ALL SELECT
'8'
UNION ALL SELECT
'9'
) b
WHERE
CONVERT(
CONCAT( a1 ,b1 ) ,
UNSIGNED
) <=(
SELECT
DAY(
CONCAT( '2012-10' ,'-01' ) + INTERVAL 1 MONTH - INTERVAL 1 DAY
)
)
AND CONCAT( a1 ,b1 ) <> '00'
) days JOIN(
SELECT
'2012-10' AS d
) dt
HAVING
dates BETWEEN '2012-10-02' AND '2012-10-05'
ORDER BY
dates
;