SQL, how to insert all rows of a table into another one

OK so I suck at SQL and I always have to ask for help, let me show my problem. I have 2 tables, here is the structure for both:

  • ID (int)
  • courseID (int)
  • course (bit)
  • bookmark (varchar(100))
  • course_date (datetime)
  • posttest (bit)
  • post_attempts (int)
  • Now I need to move all rows of one table to the other one (not update), just to have only one table with all rows of both. The ID column is a primary key with auto increment, but when I tried to use INSERT INTO table1 SELECT * FROM table2 , sql generates this error #1062 - Duplicate entry '1' for key 'PRIMARY' .


    如果table1ID上有一个标识,那么您应该选择除ID之外的所有列

    INSERT INTO table1(
        course_id,
        course,
        bookmark,
        course_date,
        posttest,
        post_attempts
    )
    SELECT
        course_id,
        course,
        bookmark,
        course_date,
        posttest,
        post_attempts
    FROM table2
    

    If you don't care about the ID from the old table transferring across, you can use this:

    INSERT INTO newTable (courseID, course, bookmark, course_date, posttest, post_attempts)
    SELECT courseID, course, bookmark, course_date, posttest, post_attempts
    FROM oldTable;
    

    This will transfer all columns except ID, which should solve the issue.

    链接地址: http://www.djcxy.com/p/94292.html

    上一篇: SQL,如何从特定的起始号码ID中插入表

    下一篇: SQL,如何将表的所有行插入到另一个表中