• 找重复值


    方便演示,我们创建一个名为contacts表,其中包含四个列:idfirst_namelast_nameemail

    表中数据如下

    | id | first_name | last_name       | email                          |
    +----+------------+-----------------+--------------------------------+
    |  1 | Carine     | Schmitt         | carine.schmitt@qq.com          |
    |  2 | Jean       | King            | jean.king@yiibai.com               |
    |  3 | Peter      | Ferguson        | peter.ferguson@google.com      |
    |  4 | Janine     | Labrune         | janine.labrune@aol.com         |
    |  5 | Jonas      | Bergulfsen      | jonas.bergulfsen@mac.com       |
    |  6 | Janine     | Labrune         | janine.labrune@aol.com         |
    |  7 | Susan      | Nelson          | susan.nelson@qq.com            |
    |  8 | Zbyszek    | Piestrzeniewicz | zbyszek.piestrzeniewicz@qq.com |
    |  9 | Roland     | Keitel          | roland.keitel@yahoo.com        |
    | 10 | Julie      | Murphy          | julie.murphy@yahoo.com         |
    | 11 | Kwai       | Lee             | kwai.lee@google.com            |
    | 12 | Jean       | King            | jean.king@qq.com               |
    | 13 | Susan      | Nelson          | susan.nelson@qq.comt           |
    | 14 | Roland     | Keitel          | roland.keitel@yahoo.com 

    在一列中找到重复的值

    1 SELECT 
    2     email, 
    3     COUNT(email)
    4 FROM
    5     contacts
    6 GROUP BY email
    7 HAVING COUNT(email) > 1;

    输出如下

    +-------------------------+--------------+
    | email                   | COUNT(email) |
    +-------------------------+--------------+
    | janine.labrune@aol.com  |            2 |
    | roland.keitel@yahoo.com |            2 |
    +-------------------------+--------------+
    2 rows in set

    在多个列中查找重复值

     1 SELECT 
     2     first_name, COUNT(first_name),
     3     last_name,  COUNT(last_name),
     4     email,      COUNT(email)
     5 FROM
     6     contacts
     7 GROUP BY 
     8     first_name , 
     9     last_name , 
    10     email
    11 HAVING  COUNT(first_name) > 1
    12     AND COUNT(last_name) > 1
    13     AND COUNT(email) > 1;

    输出

    +------------+-------------------+-----------+------------------+-------------------------+--------------+
    | first_name | COUNT(first_name) | last_name | COUNT(last_name) | email                   | COUNT(email) |
    +------------+-------------------+-----------+------------------+-------------------------+--------------+
    | Janine     |                 2 | Labrune   |                2 | janine.labrune@aol.com  |            2 |
    | Roland     |                 2 | Keitel    |                2 | roland.keitel@yahoo.com |            2 |
    +------------+-------------------+-----------+------------------+-------------------------+--------------+
    2 rows in set
  • 相关阅读:
    获取本机IP地址[JavaScript / Node.js]
    python 优雅的解析 jsonp
    Ext.jsTree 向子节点添加叶子节点
    LeetCode面试题 08.05. 递归乘法:A * B 就是B个A相加
    LeetCode第235题:二叉搜索树的最近公共祖先——到两个点路径交集的最后一个元素
    InvalidMongoDbApiUsageException之重复的字段条件
    leetcode第6题:Z字形变换--直接模拟求解法
    Arch Linux 安装指南
    从Windows文件夹到Linux分区
    华为 服务器 存储相关资料
  • 原文地址:https://www.cnblogs.com/wangzhisdu/p/8297729.html
Copyright © 2020-2023  润新知