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

时间:2012-08-09 12:26:58

标签: mysql

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

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

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

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

3 个答案:

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

答案 1 :(得分:0)

找到了一个解决方法,因为它长期坚持

BASE TABLE

 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为空,为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)

答案 2 :(得分:-1)

作为两阶段查询:

SELECT @prev := null;

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

将检测与上一行/日期差异超过1天的日期