MySQL 中特定的重复选择

发布于 2024-12-29 17:32:01 字数 1006 浏览 1 评论 0原文

我确实查看了 SO 的内部和外部,但仍然不知道这是否可以做到。我有一个如下所示的表:

User ID | Role | First Name | Last Name | Email |<br>
0001    | K    | John       | Smith     | [email protected]|<br>
0002    | Q    | Jane       | Dickens   | [email protected]|<br>
0003    | K    | John       | Smith     | [email protected]|<br>
0004    | J    | Jack       | Paper     | [email protected]|<br>

如您所见,由于用户两次单独输入信息,该表包含重复项。我想显示的行

  • 1.名字相同
  • 2. 姓氏相同
  • 3.有相同的邮箱
  • 4. 没有相同的用户 ID

    我可以获得前三个条件来使用内连接子查询,但是当我尝试添加第四个条件时,我得到 0 返回结果。

    预先感谢您的帮助!

  • I did take a look in and outside of SO and still don't know if this can be done. I have a table that looks like this:

    User ID | Role | First Name | Last Name | Email |<br>
    0001    | K    | John       | Smith     | [email protected]|<br>
    0002    | Q    | Jane       | Dickens   | [email protected]|<br>
    0003    | K    | John       | Smith     | [email protected]|<br>
    0004    | J    | Jack       | Paper     | [email protected]|<br>
    

    As you can see, the table contains a duplicate due to a user entering their information two separate times. I want to display the rows that

  • 1. have the same first name
  • 2. have the same last name
  • 3. have the same email
  • 4. do NOT have the same User ID

    I can get the first three conditions to work with an inner join subquery, but I get 0 returned results when ever I try to to add in the fourth condition.

    Thanks in advance for your help!

  • 如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(6

    后eg是否自 2025-01-05 17:32:01
    SELECT GROUP_CONCAT(`User ID`) as IDs, Role, `First Name`, `Last Name`, Email
    FROM users_table
    GROUP BY Role,`First Name`,`Last Name`,Email
    

    将给出一个像

    IDs       | Role | First Name | Last Name | Email |
    0001,0003 |   K  |    John    |   Smith   | [email protected]|
    0002      |   Q  |    Jane    |  Dickens  | [email protected]|
    0004      |   J  |    Jack    |   Paper   | [email protected]|
    

    这样的表。技巧是对除ID之外的所有内容进行GROUP BY

    您也可以这样做:

    SELECT COUNT(`User ID`) as numIDs, GROUP_CONCAT(`User ID`) as IDs, 
           Role, `First Name`, `Last Name`, Email
    FROM users_table
    GROUP BY Role,`First Name`,`Last Name`,Email
    HAVING numIDs > 1
    

    要得到

    numIDs |IDs       | Role | First Name | Last Name | Email |
        2  |0001,0003 |   K  |    John    |   Smith   | [email protected]|
    

    无论如何,您明白了如何根据您的目的改变它。

    SELECT GROUP_CONCAT(`User ID`) as IDs, Role, `First Name`, `Last Name`, Email
    FROM users_table
    GROUP BY Role,`First Name`,`Last Name`,Email
    

    Will give a table like

    IDs       | Role | First Name | Last Name | Email |
    0001,0003 |   K  |    John    |   Smith   | [email protected]|
    0002      |   Q  |    Jane    |  Dickens  | [email protected]|
    0004      |   J  |    Jack    |   Paper   | [email protected]|
    

    The trick is to GROUP BY everything except ID.

    You could also do:

    SELECT COUNT(`User ID`) as numIDs, GROUP_CONCAT(`User ID`) as IDs, 
           Role, `First Name`, `Last Name`, Email
    FROM users_table
    GROUP BY Role,`First Name`,`Last Name`,Email
    HAVING numIDs > 1
    

    to get

    numIDs |IDs       | Role | First Name | Last Name | Email |
        2  |0001,0003 |   K  |    John    |   Smith   | [email protected]|
    

    Anyhow, you get the point of how to vary it to your purposes.

    ┼── 2025-01-05 17:32:01

    尝试类似的方法:

    select *
      from tb_users
     where (first_name, last_name, email) in
       (select first_name, last_name, email
          from tb_users
         group by first_name, last_name, email
        having count(*) > 1)
    

    Try something like:

    select *
      from tb_users
     where (first_name, last_name, email) in
       (select first_name, last_name, email
          from tb_users
         group by first_name, last_name, email
        having count(*) > 1)
    
    假情假意假温柔 2025-01-05 17:32:01

    我假设您的表不包含真正的重复行(其中用户 ID 也匹配)。我认为获取相关行应该像这样简单(使用我调整后的列和表命名方案):

    SELECT ui.user_id, ui.role, ui.first_name, ui.last_name, ui.email
    FROM user_info AS ui
      INNER JOIN user_info AS udi
        ON ui.first_name = udi.first_name
          AND ui.last_name = udi.last_name
          AND ui.email = udi.email
          AND ui.user_id != udi.user_id;
    

    I am assuming that your table does not contain true duplicate rows (where the User ID also matches). I think it should be as simple as this to get the relevant rows back (with my adjusted column and table naming scheme):

    SELECT ui.user_id, ui.role, ui.first_name, ui.last_name, ui.email
    FROM user_info AS ui
      INNER JOIN user_info AS udi
        ON ui.first_name = udi.first_name
          AND ui.last_name = udi.last_name
          AND ui.email = udi.email
          AND ui.user_id != udi.user_id;
    
    噩梦成真你也成魔 2025-01-05 17:32:01

    我会使用 count(*) 和 group by 子句。

    像这样
    SELECT count(*) as count FROM table group by concat(firstname,'\n',last_name) with count=1;

    I would use count(*) and group by clause.

    Like this
    SELECT count(*) as count FROM table group by concat(firstname,'\n',last_name) having count=1;

    木緿 2025-01-05 17:32:01

    我认为您想删除重复的行。仅按分组进行计数并删除重复的行。

    select * from tb_users
         group by first_name, last_name, email
        having count(*) > 1
    

    I think you want to delete duplicate rows. Only take count by group by and delete duplicate rows.

    select * from tb_users
         group by first_name, last_name, email
        having count(*) > 1
    
    锦上情书 2025-01-05 17:32:01

    您不必下载扩展;转到左下角,单击设置,单击键盘快捷键,然后搜索“重复”。您应该看到如下图所示并分配您想要的组合键。我选择shift+D是因为我不想失去ctrl+D当前的行为,它也非常有益。

    重复选择

    You don't have to download extensions; go left bottom corner, click settings, click Keyboard Shortcuts, and search "duplication". You should see as in the below image and assign the key combination you want. I choose shift+D because I don't want to lose the current behavior of ctrl+D it is also very beneficial.

    Duplicate selection

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文