INSERT INTO … SELECT without detailing all columns

前端 未结 11 1290
不知归路
不知归路 2020-12-08 09:50

How do you insert selected rows from table_source to table_target using SQL in MySQL where:

  • Both tables have the same schema
相关标签:
11条回答
  • 2020-12-08 10:25

    Of course, primary key must be unique. It depends on what you want to achieve, but you could exclude rows with a primary key that already exists.

    INSERT INTO table_target SELECT * FROM table_source 
    WHERE table_source.id NOT IN (SELECT id FROM table_target)
    

    UPDATE: since you also need the extra rows, you should resolve the conflict first, does table_source have relationships? If not you could change those keys:

    UPDATE table_source SET id = id + 1000
    WHERE id IN (SELECT id FROM table_target)
    

    Where 1000, is a constant, big enough so they go after the end of your table.

    0 讨论(0)
  • 2020-12-08 10:31

    You can use dynamic query:

    DECLARE @Columns VARCHAR(MAX)=''
    DECLARE @Query VARCHAR(MAX)=''
    
    SELECT 
           @Columns = ISNULL(@Columns +',', '') + T.COLUMN_NAME
    FROM 
    ( 
        select name as COLUMN_NAME from sys.all_columns
        where object_id = (select object_id from sys.tables where name = 'Source_Table')
        and is_identity = 0
    )T
    
    
    set @Query = 'insert into Target_Table (' + SUBSTRING(@Columns,2 , 9999) + ') select ' + SUBSTRING(@Columns,2 , 9999) + ' from Source_Table';
    
    PRINT @Query
    EXEC(@Query)
    
    0 讨论(0)
  • 2020-12-08 10:34

    Either you list all of the fields you want in the insert...select, or you use something else externally to build the list for you.

    SQL does not have something like SELECT * except somefield FROM, so you'll have to bite the bullet and write out the field names.

    0 讨论(0)
  • 2020-12-08 10:38

    INSERT IGNORE just "bypass" the duplicate rows.

    http://dev.mysql.com/doc/refman/5.5/en/insert.html

    0 讨论(0)
  • 2020-12-08 10:41

    This is my final solution to mass update with 'replace insert' command.

    SET @@session.group_concat_max_len = @@global.max_allowed_packet;
    SET @schema_db = 'db';
    SET @tabl = 'table';
    SET @cols = (SELECT CONCAT('`',GROUP_CONCAT(COLUMN_NAME SEPARATOR '`, `'), '`') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = @schema_db AND TABLE_NAME = @tabl GROUP BY TABLE_NAME);
    SET @Querystr = CONCAT('REPLACE INTO ',@schema_db,'.',@tabl,' SELECT ', @cols,' FROM import.tbl_', @tabl);
    
    PREPARE stmt FROM @Querystr;
    EXECUTE stmt;
    
    0 讨论(0)
  • 2020-12-08 10:42

    Tedious but safe and correct.

    Writing INSERT statements without providing a list of columns leads to code that's hard to debug and, more importantly, very fragile code that will break if the definition of the table is changed.

    If you absolutely can't write the column names out yourself then it's relatively easy to build a tool into your code that will create the comma-separated list for you.

    0 讨论(0)
提交回复
热议问题