1

我有一个下面结构的表。

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

目前我有以下方式的记录。

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 <一个神奇的查询>;

+------------+------+
| 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

2

您显然需要第二张表,其中包含可能的日期列表,然后您应该从该表中选择左连接到您已有的表。

于 2012-08-10T09:40:56.053 回答
1

日历表使您的查询和生活更轻松。在标准 SQL 中,此查询将为您提供所需的内容。

select c.cal_date, coalesce(d.id, 0) id
from calendar c
left join depot d on d.recd = c.cal_date
where c.cal_date between '2012-07-01' and '2012-07-31'
order by c.cal_date

一个最小的日历表只需要一个日期列。

create table calendar (
  cal_date date primary key
);

insert into calendar values
('2012-07-01'),
('2012-07-02'),
...
('2012-07-31');

无需编写 INSERT 语句,您可以使用电子表格或脚本程序生成数据,并通过数据库的批量加载器加载行。

我还在StackOverflow 上写了一个更有用的日历表。

于 2012-08-10T11:36:59.543 回答
0

谢谢小伙伴!!如果存在的话,我对任何 SQL 都抱有雄心。但是,是的,它不情愿的程序..

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

BASE TABLE

 CREATE TABLE `deopt` (
  `recd` datetime DEFAULT NULL,
  `id` int(11) DEFAULT NULL
) ENGINE=InnoDB;
Seed records to the base table

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 a table for dates of a month

CREATE TABLE seq_dates 
(
   sdate DATETIME NOT NULL,

);
Create a Stored Procedure to create records for a called month

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 ;
Call the procedure for July month with starting and ending values to be seeded to seq_dates table.

call sp_init_dates ('2012-07-01','2012-07-31'); 
RESULT QUERY - To fetch records of all dates in a month and its corresponding ids keeping 0 inplace of null for ids.

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-10T12:03:16.270 回答