你的 time 在这里写出 date1,表名叫 temp_test。
1、生成某个国家的连续日期
with x as (select start_date + level-1 start_date
from (select country, min(date1) start_date, max(date1) end_date
from temp_test a
where a.country = 'usa'
group by a.country) temp
connect by level <= (end_date - start_date) + 1
) select * from x
2、过滤掉已有的日期
select start_date from (with x as (select start_date + level-1 start_date
from (select country, min(date1) start_date, max(date1) end_date
from temp_test a
where a.country = 'usa'
group by a.country) temp
connect by level <= (end_date - start_date) + 1
) select * from x ) xx
where xx.start_date not in (select date1 from temp_test a where a.country = 'usa')
3、插入。
insert into temp_test
(date1, country, people)
select start_date, 'usa', 0
from (with x as (select start_date + level - 1 start_date
from (select country,
min(date1) start_date,
max(date1) end_date
from temp_test a
where a.country = 'usa'
group by a.country) temp
connect by level <= (end_date - start_date) + 1)
select *
from x) xx
where xx.start_date not in
(select date1 from temp_test a where a.country = 'usa')
通过 AUTO_INCREMENT设置SQL INSERT语句的时候,要避免 指定那个自增的字段.否则会发生主键的冲突。
通过 ALTER TABLE语句 可以修改 自增的数值, 但是只能增加,不能减少。
TRUNCATE TABLE 语句,会将自增ID重置为零。
mysql> CREATE TABLE test_create_tab2 (
-> id INT AUTO_INCREMENT,
-> val VARCHAR(10),
-> PRIMARY KEY (id)
-> )
Query OK, 0 rows affected (0.09 sec)
mysql> INSERT INTO test_create_tab2(val) VALUES ('NO id')
Query OK, 1 row affected (0.03 sec)
mysql> select last_insert_id() as id
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.00 sec)
mysql> INSERT INTO test_create_tab2(val) VALUES ('NO id 2')
Query OK, 1 row affected (0.03 sec)
mysql> select last_insert_id() as id
+----+
| id |
+----+
| 2 |
+----+
1 row in set (0.00 sec)
mysql> select * from test_create_tab2
+----+---------+
| id | val |
+----+---------+
| 1 | NO id |
| 2 | NO id 2 |
+----+---------+
2 rows in set (0.00 sec)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)