ChatGPT解决这个技术问题 Extra ChatGPT

MySQL: How to copy rows, but change a few fields?

I have a large number of rows that I would like to copy, but I need to change one field.

I can select the rows that I want to copy:

select * from Table where Event_ID = "120"

Now I want to copy all those rows and create new rows while setting the Event_ID to 155. How can I accomplish this?


d
dcp
INSERT INTO Table
          ( Event_ID
          , col2
           ...
          )
     SELECT "155"
          , col2
           ...
      FROM Table WHERE Event_ID = "120"

Here, the col2, ... represent the remaining columns (the ones other than Event_ID) in your table.


is there any way to do it without having to specify the column names?
Not that I'm aware of. Of course, if your table has like 1000 columns or something and you don't want to type them all, then you could write a SQL statement to build your SQL statement :). The way you would do it would be to use the information_schema to get the column names for the table. But that's really overkill, I'd just type out the column names.
Would this be possible using an asterisk to get the remaining columns?
Logged in to upvote just to find out I have this upvoted already. I guess this saved me several times now :) Thank you!
@Bitterblue - Sorry, I don't understand your comment. I never said col2, ... were columns that you don't want to copy, I just said they represent the remaining columns from the table. It's obvious that they will be copied, otherwise, they wouldn't be in the SQL statement at all.
I
Iwazaru

This is a solution where you have many fields in your table and don't want to get a finger cramp from typing all the fields, just type the ones needed :)

How to copy some rows into the same table, with some fields having different values:

Create a temporary table with all the rows you want to copy Update all the rows in the temporary table with the values you want If you have an auto increment field, you should set it to NULL in the temporary table Copy all the rows of the temporary table into your original table Delete the temporary table

Your code:

CREATE table temporary_table AS SELECT * FROM original_table WHERE Event_ID="155";

UPDATE temporary_table SET Event_ID="120";

UPDATE temporary_table SET ID=NULL;

INSERT INTO original_table SELECT * FROM temporary_table;

DROP TABLE temporary_table;

General scenario code:

CREATE table temporary_table AS SELECT * FROM original_table WHERE <conditions>;

UPDATE temporary_table SET <fieldx>=<valuex>, <fieldy>=<valuey>, ...;

UPDATE temporary_table SET <auto_inc_field>=NULL;

INSERT INTO original_table SELECT * FROM temporary_table;

DROP TABLE temporary_table

Simplified/condensed code:

CREATE TEMPORARY TABLE temporary_table AS SELECT * FROM original_table WHERE <conditions>;

UPDATE temporary_table SET <auto_inc_field>=NULL, <fieldx>=<valuex>, <fieldy>=<valuey>, ...;

INSERT INTO original_table SELECT * FROM temporary_table;

As creation of the temporary table uses the TEMPORARY keyword it will be dropped automatically when the session finishes (as @ar34z suggested).


MySQL supports the TEMPORARY keyword to create temporary tables. Usage of CREATE TEMPORARY TABLE will automagically drop the table when the session (a serie of SQL queries) is finished. Dropping the table wouldn't be necessary and it doesn't conflict with other temporary tables using the same name. (e.g. when live hacking (which I wouldn't recommend))
this code only works if you user #temporary_table instead of temporary_table, maybe a MSSQL issue?
This basically worked perfectly for me, but I did have to overcome a Not Null constraint on the primary key in the temp table that seems to get copied from the original table. I fixed this by altering the temp table before the update as follows: ALTER TABLE temporary_table MODIFY <auto_inc_not_null_field> INT; Then the update of the primary key to Null would not fail.
I can't get it to work in PostgreSQL 9.4: Exception: null value in column <auto_inc_not_null_field> violates not-null constraint. I tried ALTER TABLE temporary_table ALTER COLUMN <auto_inc_not_null_field> DROP NOT NULL;
I tested with different MySQL and MariaDB versions. Works perfect for me and I prefer this version to the accepted answer, which actually happens quite often here :)
P
Peter Bailey

Let's say your table has two other columns: foo and bar

INSERT INTO Table (foo, bar, Event_ID)
SELECT foo, bar, "155"
  FROM Table
 WHERE Event_ID = "120"

is there any way to do it without having to specify the column names?
@PeterBailey New to MySQL, could you please shed some light as to how the code works :/
D
Davethebfg

If you have loads of columns in your table and don't want to type out each one you can do it using a temporary table, like;

SELECT *
INTO #Temp
FROM Table WHERE Event_ID = "120"
GO

UPDATE #TEMP
SET Column = "Changed"
GO

INSERT INTO Table
SELECT *
FROM #Temp

a note : this assumes that you have no primary key in your table
t
thkala

Hey how about to copy all fields, change one of them to the same value + something else.

INSERT INTO Table (foo, bar, Event_ID)
SELECT foo, bar, Event_ID+"155"
  FROM Table
 WHERE Event_ID = "120"

??????????


a
axel.becker

As long as Event_ID is Integer, do this:

INSERT INTO Table (foo, bar, Event_ID)
SELECT foo, bar, (Event_ID + 155)
  FROM Table
WHERE Event_ID = "120"

A
Aron

Adding to the answer by @DaveTheBFG: If you have an identity column ("Table_PK" in the below example), the INSERT line would fail, but you can do the following (SQL Server-specific, but the concept may apply to other databases):

SELECT *
INTO #Temp
FROM Table WHERE Event_ID = "120"

UPDATE #TEMP
SET Column = "Changed"

ALTER TABLE #TEMP DROP COLUMN Table_PK

EXEC sp_executesql N'INSERT INTO Table SELECT * FROM #Temp'