如何在一个查询中填充一年中的所有日子?

时间:2016-03-03 10:31:30

标签: mysql

我想填充我的表tblRemarks有6列名称,日期,备注,total_absents,remarks_information,Section

我的列中有数据

NAME         DATE      TOTAL_ABSENTS     REMARKS   REMARKS_INFORMATION     SECTION

lemich   3/2/2016         5            absent         CUTTING CLASSES         3A

pablo    3/2/2016         6            absent         CUTTING CLASSES         3A

lemich   3/3/2016         5           ________         ___________            3A

pablo    3/3/2016         6           ________         ___________            3A

我想生成输出

NAME         DATE      TOTAL_ABSENTS     REMARKS   REMARKS_INFORMATION     SECTION

lemich   3/2/2016         5            absent         CUTTING CLASSES         3A

pablo    3/2/2016         6            absent         CUTTING CLASSES         3A

lemich   3/3/2016         5           ________         ___________            3A

pablo    3/3/2016         6           ________         ___________            3A

lemich   3/4/2016         5           ________         ___________            3A

pablo    3/4/2016         6           ________         ___________            3A

lemich   3/5/2016         5           ________         ___________            3A

pablo    3/5/2016         6           ________         ___________            3A

lemich   3/6/2016         5           ________         ___________            3A

pablo    3/6/2016         6           ________         ___________            3A 
and so on....

意思是我想做一个插入语句,我可以用一个查询填充一年中的所有日子。

注意(备注应为空)

2 个答案:

答案 0 :(得分:1)

写入插入查询然后选择此查询

SET @count := 0;
SET @TOTAL_ABSENTS := 5;
SET @REMARK := NULL;
select CONCAT(@count := @count+1,'.', 'lemich') AS NAME, date_format(selected_date,'%m/%d/%Y') AS `DATE`, @TOTAL_ABSENTS AS TOTAL_ABSENTS, @REMARK AS REMARK from 
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date between '2016-01-01' and '2016-12-31';

这是2016年的所有日子返回

SQLFiddle

答案 1 :(得分:0)

完全取决于您想要插入的内容(例如,您希望填写一年中剩余的时间,距离最晚日期一年等),然后确切需要更改。

这将为每个名称的最新日期添加365天的行数。每个名称的部分值只是一个随机值,但您没有指定每个名称是否有多个名称,如果是,则选择哪一个。

未经测试,但在这里你去了

INSERT INTO tblRemarks(`name`, date, total_absents, remarks, remarks_information, section)
SELECT sub_names.`name`, 
        DATE_ADD(sub_names.max_date, INTERVAL hundreds.i * 100 + tens.i * 10 + units.i + 1 DAY),
        sub_names.total_absents,
        NULL,
        NULL,
        a_section
FROM
(
    SELECT `name`, MAX(total_absents) AS total_absents, MAX(`date`) AS max_date, SUBSTRING_INDEX(GROUP_CONCAT(section), ',', 1) AS a_section
    FROM tblRemarks 
    GROUP BY `name`
) sub_names
CROSS JOIN
(
    SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 
) units
CROSS JOIN
(
    SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 
) tens
CROSS JOIN
(
    SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 
) hundreds
WHERE hundreds.i * 100 + tens.i * 10 + units.i < 365