Blogger Information
Blog 28
fans 0
comment 0
visits 65351
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
复制mysql数据表里的多条记录,并插入表里面
蒸蒸
Original
1148 people have browsed it

一、mysql数据库,复制表里多条数据,并插入到数据表里面
① INSERT INTO power_node(title,type,status) SELECT title,type,status FROM power_node WHERE id < 5;

② INSERT into jiaban (num,overtime) SELECT num,overtime from jiaban where id IN(1,3,5,6,7,9);
引用:https://blog.csdn.net/weixin_29695125/article/details/113208510

当需要复制的字段较少时,直接使用上面的方法,比较方便。

二、当需要复制的字段较多时,可先采用下面的方法,获取以逗号分隔的字段名称:
获取数据表所有字段名,并以逗号分隔
SELECT GROUP_CONCAT(COLUMN_NAME SEPARATOR “,”) FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = ‘db_name‘ AND TABLE_NAME = ‘table_name
修改加粗部分为自己数据库的名字和表名字
引用自:https://www.cnblogs.com/a2b1/p/7352662.html

注:
1.有的字段名与mysql的关键字有冲突,编写mysql语句的时候需要用ESC键下面的着重号(``)引起来,否则会报错
2.多个where条件用and连接,比如where id>5 and id<10 3.如果数据表有主键,是不可以复制所有字段的,因为主键的唯一的。如果数据表没有主键,可以使用INSERT into jiaban SELECT * from jiaban where id>5;复制所有字段,就不用查询字段了

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post