简体   繁体   English

mySQL - 根据另一列中的重复项查找唯一列

[英]mySQL - Find find unique columns based on duplicates in another column

I have been investigating this topic but it is not quite what I need and I do not understand how to solve it.我一直在研究这个话题,但这并不是我所需要的,我不明白如何解决它。

Table: companies表:公司

Columns: id, cust_number, cust_name列:id、cust_number、cust_name

There may be many records with the same cust_number, but all such records should have identical cust_name.可能有许多具有相同 cust_number 的记录,但所有这些记录都应该具有相同的 cust_name。

The query I am trying to write should display all records where this is not the case - like this:我正在尝试编写的查询应该显示所有不是这种情况的记录 - 像这样:

| id | cust_number | cust_name |
| -- | ----------- | --------- | 
| 10 |    800      | Acme LTD  | 
| 11 |    700      | Globex    | 
| 12 |    800      | Acme LTD  | 
| 13 |    444      | Globex    | 
| 14 |    800      | Acme LTT  | 

From the table above the query should result in:从上表中,查询应导致:

| id | cust_number | cust_name |
| -- | ----------- | --------- | 
| 10 |    800      | Acme LTD  | 
| 12 |    800      | Acme LTD  | 
| 14 |    800      | Acme LTT  | 

Because there are more than 1 records with the same cust_number but all 3 records does not have identical cust_name.因为有超过 1 条记录具有相同的 cust_number 但所有 3 条记录都没有相同的 cust_name。

Thanks for all help!感谢所有帮助!

You can use EXISTS to check for records with the same cust_number but different name.您可以使用EXISTS检查具有相同cust_number但名称不同的记录。

SELECT c1.id,
       c1.cust_number,
       c1.cust_name
       FROM companies c1
       WHERE EXISTS (SELECT *
                            FROM companies c2
                                 WHERE c2.cust_number = c1.cust_number
                                       AND c2.cust_name <> c1.cust_name);

Assuming MySQL 8.0, you can use window functions for this:假设 MySQL 8.0,您可以为此使用窗口函数:

select id, cust_number, cust_name
from (
    select 
        t.*,
        min(cust_name) over(partition by cust_number) min_cust_name,
        max(cust_name) over(partition by cust_number) max_cust_name
    from mytable t
) t
where min_cust_name <> max_cust_name

Probably an issue you're running into here is that (usually) string comparison in MySQL is case-insensitive - see " How can I make SQL case sensitive string comparison on MySQL? ".您在这里遇到的一个问题可能是 MySQL 中的(通常)字符串比较不区分大小写 - 请参阅“ 如何在 MySQL 上进行 SQL 区分大小写的字符串比较? ”。

ie SELECT ('Acme LTD' = 'Acme Ltd');SELECT ('Acme LTD' = 'Acme Ltd'); returns 1 or TRUE .返回 1 或TRUE

You can circumvent this by comparing the binary representation of the strings;您可以通过比较字符串的二进制表示来规避这一点; SELECT (BINARY 'Acme LTD' = BINARY 'Acme Ltd'); returns 0.返回 0。


With that in mind, the following query should return what you're looking for;考虑到这一点,以下查询应返回您要查找的内容;

SELECT DISTINCT t1.id
              , t1.cust_number
              , t1.cust_name
FROM my_table t1
         JOIN my_table t2 ON t2.cust_number = t1.cust_number AND
                             (BINARY t2.cust_name <> BINARY t1.cust_name)
ORDER BY t1.id;

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

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