How to find duplicates in 2 columns not 1(如何在 2 列而不是 1 列中查找重复项)
问题描述
我有一个 MySQL 数据库表,其中包含我感兴趣的两列.单独地,他们每个人都可以有重复项,但他们永远不应该有两个具有相同值的重复项.
I have a MySQL database table with two columns that interest me. Individually they can each have duplicates, but they should never have a duplicate of BOTH of them having the same value.
stone_id
可以有重复,只要每个 upsharge
的标题不同,并且相反.但是例如 stone_id
= 412 和 upcharge_title
= "sapphire" 组合应该只出现一次.
stone_id
can have duplicates as long as for each upsharge
title is different, and in reverse. But say for example stone_id
= 412 and upcharge_title
= "sapphire" that combination should only occur once.
没关系:
stone_id = 412 upcharge_title = "c2FwcGhpcmU="
stone_id = 412 upcharge_title = "cnVieQ=="
这不行:
stone_id = 412 upcharge_title = "c2FwcGhpcmU="
stone_id = 412 upcharge_title = "c2FwcGhpcmU="
是否有可以在两个字段中找到重复项的查询?如果可能的话,有没有办法将我的数据库设置为不允许这样做?
Is there a query that will find duplicates in both fields? And if possible is there a way to set my data-base to not allow that?
我使用的是 MySQL 4.1.22 版
I am using MySQL version 4.1.22
推荐答案
你应该在两个字段之间设置一个复合键.这需要每行都有一个唯一的 stone_id 和 upcharge_title.
You should set up a composite key between the two fields. This will require a unique stone_id and upcharge_title for each row.
只要找到现有的重复项,试试这个:
As far as finding the existing duplicates try this:
select stone_id,
upcharge_title,
count(*)
from your_table
group by stone_id,
upcharge_title
having count(*) > 1
这篇关于如何在 2 列而不是 1 列中查找重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 2 列而不是 1 列中查找重复项


- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- SQL 临时表问题 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01