简体   繁体   English

复制同一表中的行并仅更新MySQL中的一列

[英]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.

相关问题 SQL MySQL UPDATE TABLE将一个列复制到同一表中的其他列,这样第一列的第n行=第二列的n + 1行? - SQL MySQL UPDATE TABLE copy one column to other column in the same table such that nth row of first column = n+1th row of second column? SQL-将行从一个表复制到另一个表并同时更新目标表中的列 - SQL - Copy row from one table to another and update column in destination table at the same time 从MySQL表的一列获取字符串值,并更新同一行的另一列值 - Get string value from one column of MySQL Table and update its other column value of same row 使用mysql中相同表的其他列的行更新行 - Update row with values of other column's row of same Table in mysql 在MySQL中,我可以复制一行插入到同一张表中吗? - In MySQL, can I copy one row to insert into the same table? MYSQL-将一列复制到同一表中的另一列-有条件 - MYSQL - copy one column to another in the same table - with conditions 如何在同一表上的mysql中将数据复制到另一列? - how to copy one column to another with data in mysql on same table? MYSQL:如何将整行从一个表复制到 mysql 中的另一个表,第二个表有一个额外的列? - MYSQL: How to copy an entire row from one table to another in mysql with the second table having one extra column? 在mysql中的一个连接中选择和更新相同的表和行 - Select and update same table and row in one connection in mysql 数一列并在mysql的同一表中更新另一列 - count one column and update another in same table in mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM