如果mysql表中不存在则添加新条目

时间:2012-08-10 09:25:41

标签: sql

我有一张下面结构的表格。

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日),并且0到值id对应的错过日期。

选择<一个神奇的查询&gt ;;

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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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上写过a more useful calendar table

答案 2 :(得分: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)