问题描述
mysql> desc a;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| id | int(11) | NO | PRI | 0 | || name | varchar(10) | YES | | NULL | || pwd | int(11) | YES | | NULL | || sss | int(11) | YES | | NULL | |+-------+-------------+------+-----+---------+-------+假设有如下值:mysql> select * from a;+----+-------+------+------+| id | name | pwd | sss |+----+-------+------+------+| 2 | aaaa | 11 | 12 || 3 | bbbbb | 34 | 1 || 4 | cccc | 4 | 5 || 5 | wa | 32 | 22 || 10 | aaaa | 12 | 13 |+----+-------+------+------+请问 我要插入一条id为 12 其它字段的值和id为4的值一样 用SQL如何实现?注意表结构 id为主键 不是自增的.如果id为自增 可以用 insert into a select * from a where id=4实现.
解决方案
insert into a(id,name,pwd,sss) select 12,a.name,a.pwd,a.sss from a where a.id = 4
解决方案二:
insert into a(id,name,pwd,sss) values(12,select name,pwd,sss from a where id=4)oracle测试是OK的~ mysql可以试试看~