Copy data from one existing row to another existing row in SQL?

后端 未结 6 1273
猫巷女王i
猫巷女王i 2020-12-29 18:12

I have a table full of tracking data for as specific course, course number 6.

Now I have added new tracking data for course number 11.

Each row of data is fo

相关标签:
6条回答
  • 2020-12-29 18:22

    Maybe I read the problem wrong, but I believe you already have inserted the course 11 records and simply need to update those that meet the criteria you listed with course 6's data.

    If this is the case, you'll want to use an UPDATE...FROM statement:

    UPDATE MyTable
    SET
        complete = 1,
        complete_date = newdata.complete_date,
        post_score = newdata.post_score
    FROM
        (
        SELECT
            userID,
            complete_date,
            post_score
        FROM MyTable
        WHERE
            courseID = 6
            AND complete = 1
            AND complete_date > '8/1/2008'
        ) newdata
    WHERE
        CourseID = 11
        AND userID = newdata.userID
    

    See this related SO question for more info

    0 讨论(0)
  • 2020-12-29 18:26

    Try this:

    UPDATE barang
    SET ID FROM(SELECT tblkatalog.tblkatalog_id FROM tblkatalog 
    WHERE tblkatalog.tblkatalog_nomor = barang.NO_CAT) WHERE barang.NO_CAT <>'';
    
    0 讨论(0)
  • 2020-12-29 18:27

    This works well for coping entire records.

    UPDATE your_table
    SET new_field = sourse_field
    
    0 讨论(0)
  • 2020-12-29 18:28

    Copy a value from one row to any other qualified rows within the same table (or different tables):

    UPDATE `your_table` t1, `your_table` t2
    SET t1.your_field = t2.your_field
    WHERE t1.other_field = some_condition
    AND t1.another_field = another_condition
    AND t2.source_id = 'explicit_value'
    

    Start off by aliasing the table into 2 unique references so the SQL server can tell them apart

    Next, specify the field(s) to copy.

    Last, specify the conditions governing the selection of the rows

    Depending on the conditions you may copy from a single row to a series, or you may copy a series to a series. You may also specify different tables, and you can even use sub-selects or joins to allow using other tables to control the relationships.

    0 讨论(0)
  • 2020-12-29 18:37

    Use SELECT to Insert records

    INSERT tracking (userID, courseID, course, bookmark, course_date, posttest, post_attempts, post_score, post_date, complete, complete_date, exempted, exempted_date, exempted_reason, emailSent) 
    SELECT userID, 11, course, bookmark, course_date, posttest, post_attempts, post_score, post_date, complete, complete_date, exempted, exempted_date, exempted_reason, emailSent
    FROM tracking WHERE courseID = 6 AND course_date > '08-01-2008'
    
    0 讨论(0)
  • 2020-12-29 18:40
    UPDATE c11
    SET
        c11.completed= c6.completed,
        c11.complete_date = c6.complete_date,
    -- rest of columns to be copied
    FROM courses c11 inner join courses c6 on
        c11.userID = c6.userID 
        and c11.courseID = 11 and c6.courseID = 6
         -- and any other checks
    

    I have always viewed the From clause of an update, like one of a normal select. Actually if you want to check what will be updated before running the update, you can take replace the update parts with a select c11.*. See my comments on the lame duck's answer.

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