-1

好的,让我重新构建我的相同问题,以达到正确的输出。

我有一个下面结构的表。

mysql> desc depot;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| recd  | date     | YES  |     | NULL    |       |
| id    | int(11)  | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+

Currently I have records in the below manner.


mysql> select * from depot;
+---------------------+------+
| recd                | id   |
+---------------------+------+
| 2012-07-09          |   33 |
| 2012-07-11          |   32 |
| 2012-07-15          |   32 |
+---------------------+------+
3 rows in set (0.00 sec)

我需要记录以下列方式打印查询,保留一个月日期的错过条目(比如 7 月 1 日至 7 月 31 日),并将值 id 设为 0 对应的错过日期。

select < a magical query >;

+------------+------+
| recd       | id   |
+------------+------+
 2012-07-01 0
 2012-07-02 0
 2012-07-03 0
 2012-07-04 0
 2012-07-05 0
 2012-07-06 0
 2012-07-07 0
 2012-07-08 0
 2012-07-09 33
 2012-07-10 0
 2012-07-11 32
 2012-07-12 0
 2012-07-13 0
 2012-07-14 0
 2012-07-15 32
 2012-07-16 0
 2012-07-17 0
 2012-07-18 0
 2012-07-19 0
 2012-07-20 0
 2012-07-21 0
 2012-07-22 0
 2012-07-23 0
 2012-07-24 0
 2012-07-25 0
 2012-07-26 0
 2012-07-27 0
 2012-07-28 0
 2012-07-29 0
 2012-07-30 0
 2012-07-31 0
4

3 回答 3

0
SELECT
generated_date,
COALESCE(yourTable.id, 0) AS id
FROM
(
SELECT
DATE_SUB(CURDATE(), INTERVAL number_days DAY) AS `generated_date` /*<-- Create dates from now back 999 days*/
FROM
(
SELECT (a + 10*b + 100*c) AS number_days FROM
  (SELECT 0 AS a 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) aa
, (SELECT 0 AS b 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) bb
, (SELECT 0 AS c 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) cc
)sq /*<-- This generates numbers 0 to 999*/
) q
LEFT JOIN yourTable ON DATE(yourTable.datefield) = q.generated_date
WHERE q.generated_date BETWEEN (SELECT MIN(datefield) FROM yourTable) AND (SELECT MAX(datefield) FROM yourTable)
ORDER BY q.generated_date ASC
于 2012-08-09T13:38:41.623 回答
0

找到了解决方法,因为它坚持了很长时间

基表

 CREATE TABLE `deopt` (
  `recd` datetime DEFAULT NULL,
  `id` int(11) DEFAULT NULL
) ENGINE=InnoDB;

将记录播种到基表

insert into deopt values ('2012-07-09 23:08:54',22);
insert into deopt values ('2012-07-11 23:08:54',22);
insert into deopt values ('2012-07-11 23:08:54',2222);
insert into deopt values ('2012-07-12 23:08:54',22);
insert into deopt values ('2012-07-14 23:08:54',245);

为一个月的日期创建一个表

CREATE TABLE seq_dates 
(
   sdate DATETIME NOT NULL,

);

创建存储过程以创建调用月份的记录

delimiter //
DROP PROCEDURE IF EXISTS sp_init_dates;

CREATE PROCEDURE sp_init_dates (IN p_fdate DATETIME, IN p_tdate DATETIME)
BEGIN 
DECLARE v_thedate DATETIME; 
TRUNCATE TABLE seq_dates; 
SET v_thedate = p_fdate;
WHILE (v_thedate <= p_tdate) DO 
   INSERT INTO seq_dates (sdate) 
   VALUES (v_thedate); 
   SET v_thedate = DATE_ADD(v_thedate, INTERVAL 1 DAY); 
END WHILE; 
END;

delimiter ;

调用 7 月份的过程,并将开始和结束值播种到 seq_dates 表。

call sp_init_dates ('2012-07-01','2012-07-31'); 

结果查询 - 获取一个月内所有日期的记录及其对应的 id,保持 0 代替 id 的空值。

select date(seq_dates.sdate),coalesce (deopt.id,0) from seq_dates LEFT JOIN deopt ON date(deopt.recd)=date(seq_dates.sdate);



+-----------------------+-----------------------+
| date(seq_dates.sdate) | coalesce (deopt.id,0) |
+-----------------------+-----------------------+
| 2012-07-01            |                     0 |
| 2012-07-02            |                     0 |
| 2012-07-03            |                     0 |
| 2012-07-04            |                     0 |
| 2012-07-05            |                     0 |
| 2012-07-06            |                     0 |
| 2012-07-07            |                     0 |
| 2012-07-08            |                     0 |
| 2012-07-09            |                    22 |
| 2012-07-09            |                    22 |
| 2012-07-10            |                     0 |
| 2012-07-11            |                    22 |
| 2012-07-11            |                  2222 |
| 2012-07-11            |                    22 |
| 2012-07-11            |                  2222 |
| 2012-07-12            |                    22 |
| 2012-07-13            |                     0 |
| 2012-07-14            |                   245 |
| 2012-07-15            |                     0 |
| 2012-07-16            |                     0 |
| 2012-07-17            |                     0 |
| 2012-07-18            |                     0 |
| 2012-07-19            |                     0 |
| 2012-07-20            |                     0 |
| 2012-07-21            |                     0 |
| 2012-07-22            |                     0 |
| 2012-07-23            |                     0 |
| 2012-07-24            |                     0 |
| 2012-07-25            |                     0 |
| 2012-07-26            |                     0 |
| 2012-07-27            |                     0 |
| 2012-07-28            |                     0 |
| 2012-07-29            |                     0 |
| 2012-07-30            |                     0 |
| 2012-07-31            |                     0 |
+-----------------------+-----------------------+
35 rows in set (0.00 sec)
于 2012-08-10T11:59:12.000 回答
-1

作为两阶段查询:

SELECT @prev := null;

SELECT datefield, @prev, DATEDIFF(datefield, @prev) AS diff, @prev := datefield
FROM yourtable
HAVING diff > 1
ORDER BY datefield ASC;

这将检测与前一行/日期相差超过 1 天的日期

于 2012-08-09T12:35:45.993 回答