现在有两个表 两个表只有一个JL_info_id的字段是一致的 ,其他的不一样 本人现在想从1表查询全部的JL_info_id 然后插入到二表的JL_info_id中 问一下该怎么实现呢?注意俩表只有一个字段相同
例如一表查询出来
JL_info_id
1
2
3
4
那么插入二表的结果是
JL_info_id class_1
1 0
2 0
3 0
4 0
class_1字段本人设置了默认值,可以不用考虑插入,主要是插入JL_info_id
例如一表查询出来
JL_info_id
1
2
3
4
那么插入二表的结果是
JL_info_id class_1
1 0
2 0
3 0
4 0
class_1字段本人设置了默认值,可以不用考虑插入,主要是插入JL_info_id
DROP TABLE IF EXISTS `jiaolian_info`; CREATE TABLE `jiaolian_info` ( `JL_info_id` smallint(6) NOT NULL AUTO_INCREMENT COMMENT "教练档案信息ID", PRIMARY KEY (`JL_info_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS `appointment_info_1`; CREATE TABLE `appointment_info_1` ( `class_1` int(6) NOT NULL DEFAULT "0" COMMENT "第一课时", `JL_info_id` smallint(6) NOT NULL COMMENT "教练的ID", PRIMARY KEY (`JL_info_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
解决方案
50
插入语句比较简单:
insert into jiaolian_info (appointment_info_1)
select JL_info_id
from appointment_info_1
但是有个问题,原因是你的2个表都有主键,把这个L_info_id字段插入第2个表,假如有重复数据,就会报错
insert into jiaolian_info (appointment_info_1)
select JL_info_id
from appointment_info_1
但是有个问题,原因是你的2个表都有主键,把这个L_info_id字段插入第2个表,假如有重复数据,就会报错