他们是ANSI标准中的一部分,因此也可以用在其他数据库中。而<=>只能在mysql中使用。
也就是说不用管每条记录的id会不会变咯?方法一:使用select ...into outfile 和 load data infile ...
1.导出除了字段id的所有字段
2.清空表
3.导入数据
例子:
#建立测试表 lt1
mysql>create table lt1(id int(10) auto_increment,v1 int(10) default 0,v2 int(10
) default 0,primary key(id))engine=innodb,default charset=utf8
Query OK, 0 rows affected (0.11 sec)
#插入测试数据
mysql>insert into lt1(v1,v2) values(1,1),(2,2),(3,3),(4,4),(5,5)
Query OK, 5 rows affected (0.06 sec)
Records: 5 Duplicates: 0 Warnings: 0
#删除id=4的数据
mysql>delete from lt1 where id=4
Query OK, 1 row affected (0.08 sec)
#查看测试表的数据,这时候id不连续。
mysql>select * from lt1
+----+------+------+
| id | v1 | v2 |
+----+------+------+
| 1 |1 |1 |
| 2 |2 |2 |
| 3 |3 |3 |
| 5 |5 |5 |
+----+------+------+
4 rows in set (0.00 sec)
#将表中的v1,v2字段导出到lt1.xls中。
mysql>select v1,v2 from lt1 into outfile 'd:/localdmp/data/lt1.xls'
Query OK, 4 rows affected (0.03 sec)
#清空lt1
mysql>truncate table lt1
Query OK, 0 rows affected (0.08 sec)
#从lt1.xls文件中导入数据到lt1中,注意指定字段名(v1,v2)。
mysql>load data infile 'd:/localdmp/data/lt1.xls' into table lt1(v1,v2)
Query OK, 4 rows affected (0.08 sec)
Records: 4 Deleted: 0 Skipped: 0 Warnings: 0
#现在id连续了
mysql>select * from lt1
+----+------+------+
| id | v1 | v2 |
+----+------+------+
| 1 |1 |1 |
| 2 |2 |2 |
| 3 |3 |3 |
| 4 |5 |5 |
+----+------+------+
4 rows in set (0.00 sec)
方法二:
使用临时表create table ...select ... 和 insert ...select ...
例子:
#还是lt1表,先清空
mysql>truncate table lt1
Query OK, 0 rows affected (0.08 sec)
#插入测试数据,id 1-5
mysql>insert into lt1(v1,v2) values(1,1),(2,2),(3,3),(4,4),(
Query OK, 5 rows affected (0.06 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql>select * from lt1
+----+------+------+
| id | v1 | v2 |
+----+------+------+
| 1 |1 |1 |
| 2 |2 |2 |
| 3 |3 |3 |
| 4 |4 |4 |
| 5 |5 |5 |
+----+------+------+
5 rows in set (0.00 sec)
#把id=4删除
mysql>delete from lt1 where id=4
Query OK, 1 row affected (0.06 sec)
mysql>select * from lt1
+----+------+------+
| id | v1 | v2 |
+----+------+------+
| 1 |1 |1 |
| 2 |2 |2 |
| 3 |3 |3 |
| 5 |5 |5 |
+----+------+------+
4 rows in set (0.00 sec)
#创建临时表lt11
mysql>create table lt11 select v1,v2 from lt1
Query OK, 4 rows affected (0.11 sec)
Records: 4 Duplicates: 0 Warnings: 0
#临时表的数据
mysql>select *from lt11
+------+------+
| v1 | v2 |
+------+------+
|1 |1 |
|2 |2 |
|3 |3 |
|5 |5 |
+------+------+
4 rows in set (0.00 sec)
#清空lt1
mysql>truncate table lt1
Query OK, 0 rows affected (0.06 sec)
#将临时表lt11的数据插入lt1中
mysql>insert into lt1 (v1,v2) select v1,v2 from lt11
Query OK, 4 rows affected (0.06 sec)
Records: 4 Duplicates: 0 Warnings: 0
#查看结果,id已经连续。
mysql>select *from lt1
+----+------+------+
| id | v1 | v2 |
+----+------+------+
| 1 |1 |1 |
| 2 |2 |2 |
| 3 |3 |3 |
| 4 |5 |5 |
+----+------+------+
4 rows in set (0.00 sec)
唔,这么辛苦,给个最佳至少啊...
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)