一列是唯一的INSERT SELECT查询 [英] INSERT SELECT query when one column is unique

查看:209
本文介绍了一列是唯一的INSERT SELECT查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用INSERT + SELECT将用户数据导入到不同的表中,我需要过滤user_name的重复,所以我需要这样的东西:

I need to call INSERT + SELECT to import user data to different table and I need to filter user_name duplications, So I need something like this:

INSERT INTO new_table SELECT email, distinct user_name, password from old_table

但不同的作品只有使用不同的电子邮件,user_name,密码和所有这些列需要是唯一的。

but distinct works only when using distinct email, user_name, password and all those column need to be unique.

有没有其他使用uniq user_names插入select的方式(我只需要第一行 - 具有较低的id)?

Is there any other way to insert select with uniq user_names, (I need only first row - with lower id)?

编辑我忘了提到我使用mysql

EDIT I forget to mention that I use mysql

推荐答案

在没有测试的情况下,我会期待这样的事情来做这个伎俩(假设id列被命名为id) >

Without testing I would expect something like this to do the trick (assuming the id column is named id)

INSERT INTO new_table 
    SELECT email, user_name, password 
    FROM old_table 
    INNER JOIN 
        ( SELECT MIN(id) FROM old_table GROUP by user_name ) minids
    ON minids.id = old_table.id

这篇关于一列是唯一的INSERT SELECT查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆