简体   繁体   English

在mysql中使用select语句插入表中

[英]Insert into a table with a select statement in mysql

I have a table that has training history that has been modified by many different users over the years. 我有一个表格,其中包含过去几年中许多不同用户修改的培训历史记录。 This has cause the same training record to be entered twice. 这导致两次输入相同的训练记录。 I want to create a table that replicates the main table and insert all duplicate records. 我想创建一个复制主表并插入所有重复记录的表。

What constitutes a duplicate record is if the employee_id, course_code, and completion_date all match. 如果employee_id,course_code和completion_date全部匹配,则构成重复记录。

I can create the duplicate table and I have a select statement that appears to pull duplicates, but it pulls only one of them and I need it to pull both (or more) of them. 我可以创建重复表,并且有一条select语句似乎可以拉出重复项,但是它只拉出其中一个,因此我需要它来拉出两个(或多个)它们。 This is because one person may have entered the training record with a different course name but the id, code, and date are the same so it is a duplicate entry. 这是因为一个人可能以不同的课程名称输入了培训记录,但是id,代码和日期相同,因此是重复的输入。 So by pulling all the duplicates I can validate that that is the case. 因此,通过拉出所有重复项,我可以验证情况是否如此。

Here is my SELECT statement: 这是我的SELECT语句:

SELECT * 
FROM 
    training_table p1
JOIN 
    training_table p2 ON (
        p1.employee_id = p2.employee_id
        AND p1.course_code = p2.course_code
        AND p1.completion.date = p2.completion_date)
GROUP BY p1.ssn;

The query runs and returns what appear to be unique rows. 查询运行并返回看起来唯一的行。 I would like all of the duplicates. 我想要所有重复的东西。 And whenever I try to INSERT it into an identical table I get an error stating my column count doesn't match my value count. 每当我尝试将其插入到相同的表中时,都会收到一条错误消息,指出我的列数与我的值数不匹配。

Any help would be great. 任何帮助都会很棒。

This will select any duplicate rows for insertion into your new table. 这将选择任何重复的行以插入到新表中。

SELECT p1.* 

FROM   training_table p1

JOIN   
       (SELECT employee_id, course_code, completion_date
        FROM   training_table 
        GROUP BY employee_id, course_code, completion_date
        HAVING COUNT(*) > 1
       ) dups 
        ON  p1.employee_id = dups.employee_id
        AND p1.course_code = dups.course_code
        AND p1.completion_date = dups.completion_date
;

Try to use CROSS JOIN (Cartesian Product Join) instead JOIN only. 尝试使用CROSS JOIN(笛卡尔积连接),而不是仅JOIN。 For insert try INSERT INTO TABLE (column1, column2, column3) SELECT column1, column2, column3 FROM TABLE; 对于插入,请尝试INSERT INTO TABLE(column1,column2,column3)从表中选择column1,column2,column3; in same order. 以相同的顺序。

Thanks for the help. 谢谢您的帮助。 I had discovered the answer shortly after I posted the question (even though I had looked for the answer for over an hour :) ) Here is what I used: 发布问题后不久,我就找到了答案(即使我已经寻找了一个多小时的答案:))这是我使用的方法:

 SELECT  *
    FROM    training_table mto
    WHERE   EXISTS
            (
    SELECT  1
    FROM    training_table mti


    WHERE   mti.employee_id = mto.employee_ie
    AND mti.course_code = mto.course_code
    AND mti.completion_date = mto.completion_date
            LIMIT 1, 1
            )

I just added the INSERT statement and it worked. 我刚刚添加了INSERT语句,它起作用了。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM