如何将两个表合并成第三个表sql而不重写?

vulvrdjw  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(407)

所以我有3个sql表,我运行这个命令

INSERT INTO `wp_atkv_EWD_OTP_Customers` (`Customer_ID`,`Customer_Email`,`Customer_Created`) 
SELECT 
    `User_ID`, 
    `Username` , 
    `User_Date_Created` 
FROM `wp_atkv_EWD_FEUP_Users`

它工作得很好,我想用相同的用户id从另一个表中添加数据,但是我运行下面的命令,它说重复错误?我不应该使用insert-into吗?另外,如果我忽略了重复,它只会创建新的条目,我希望它们都在同一行中

INSERT INTO `wp_atkv_EWD_OTP_Customers` (`Customer_ID`,`Customer_Name`) 
SELECT 
    `User_ID`, 
    group_concat(`Field_Value`) 
FROM `wp_atkv_EWD_FEUP_User_Fields` 
GROUP by `User_ID`

我该怎么做?如何更新第三个表?

axzmvihb

axzmvihb1#

我在等一个 join :

INSERT INTO wp_atkv_EWD_OTP_Customers (Customer_ID, Customer_Email, Customer_Created, Customer_Name)
    SELECT u.User_ID, u.Username, u.User_Date_Created, uf.vals
    FROM wp_atkv_EWD_FEUP_Users u LEFT JOIN
         (SELECT User_ID, group_concat(Field_Value) as vals 
          FROM wp_atkv_EWD_FEUP_User_Fields
          GROUP by User_ID
         ) uf
         ON u.user_id = uf.user_id;

或者,您可能需要 update :

update wp_atkv_EWD_OTP_Customers c join
       (select `User_ID`, group_concat(`Field_Value`) as vals
        from `wp_atkv_EWD_FEUP_User_Fields` 
        group by `User_ID`
       ) uf
       on c.customer_id_id = cf.user_id
    set Customer_Name = uf.vals;

相关问题