[英]copy row in same table and update just one column in MySQL
I need to copy a row based on some condition(where clause), from a table,make a duplicate entry in same table and update just one column(a foreign_key). 我需要根据某个条件(where子句)从表中复制一行,在同一表中进行重复输入,并仅更新一列(foreign_key)。 But because "select * from Table_name" returns all fields including primary_key it is not able to insert new row error : duplicate key.
但是因为“从表名中选择*”会返回包括primary_key在内的所有字段,所以无法插入新行错误:重复键。
I don't know all the columns of table, so i cant ignore primary_key by giving only required column names in select query. 我不知道表的所有列,因此我无法通过在select查询中仅提供必需的列名来忽略primary_key。
Is there any way to ignore primary_key from fetching itself.. or can i fetch the whole row, and set the primary_key value to null, and then insert the row in table, so that it auto increments the primary_key for new row being added. 有什么办法可以忽略primary_key本身的获取..还是我可以获取整行,并将primary_key的值设置为null,然后将该行插入表中,以便它为要添加的新行自动递增primary_key。
Thanks. 谢谢。
It is possible to do what you want by querying the information schema tables, and using dynamic SQL to build an appropriate statement. 通过查询信息模式表并使用动态SQL来构建适当的语句,可以做您想要的事情。 However, you should take great care with this approach, as modifying data in tables for which you do not know the column names could result all sorts of problems:
但是,您应该格外小心,因为修改您不知道列名的表中的数据可能会导致各种问题:
DROP TABLE mytable;
CREATE TABLE `mytable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`col1` varchar(255) DEFAULT NULL,
`col2` varchar(255) DEFAULT NULL,
`col3` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
INSERT INTO mytable (col1, col2, col3) VALUES ('A', 'B', 'C');
SELECT * FROM mytable;
+----+------+------+------+
| id | col1 | col2 | col3 |
+----+------+------+------+
| 1 | A | B | C |
+----+------+------+------+
SET @columns := (
SELECT
GROUP_CONCAT(column_name)
FROM information_schema.columns
WHERE table_schema = 'test'
AND table_name = 'mytable'
AND column_key <> 'PRI'
);
SET @sql := (
SELECT CONCAT(
'INSERT INTO mytable (', @columns, ') ',
'SELECT ', @columns, ' FROM mytable ',
'WHERE id = 1;'
)
);
SELECT @sql;
+---------------------------------------------------------------------------------------+
| @sql |
+---------------------------------------------------------------------------------------+
| INSERT INTO mytable (col1,col2,col3) SELECT col1,col2,col3 FROM mytable WHERE id = 1; |
+---------------------------------------------------------------------------------------+
PREPARE stmt FROM @sql;
EXECUTE stmt;
SELECT * FROM mytable;
+----+------+------+------+
| id | col1 | col2 | col3 |
+----+------+------+------+
| 1 | A | B | C |
| 2 | A | B | C |
+----+------+------+------+
if you have knowledge about the sequences of the array you got, then after putting some kind of condition you can ignore field which is having primary key. 如果您了解所获得数组的序列,那么在放入某种条件后,您可以忽略具有主键的字段。
Check like this if it works 像这样检查是否可行
Thanks. 谢谢。
How can it be that you know what the key is but don't know what the other columns are? 您怎么可能知道键是什么,却不知道其他列是什么? If that's really the case then I think you'll have to write some dynamic SQL - or better still, give the task to someone who does know the table structure.
如果真是这样,那么我认为你必须写一些动态SQL -或者更好的是,给任务的人谁不知道表结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.