MySQL 检查一对多关系中的重复条目
MySQL check for duplicate entries in one-to-many relationship
我有两个 table 一对多关系。我想检查映射 table.
中是否有任何重复条目
客户:
+----------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------------+--------------+------+-----+---------+----------------+
| uid | int(11) | NO | PRI | | auto_increment |
| active | tinyint(1) | NO | | | |
| locked | tinyint(1) | NO | | | |
+----------------------------+--------------+------+-----+---------+----------------+
items_mapping:
+--------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+----------------+
| uid | int(11) | NO | PRI | | auto_increment |
| customer_uid | int(11) | NO | | | |
| device_uid | int(11) | NO | | | |
| offered | float(11,2) | NO | | | |
+--------------+---------------+------+-----+---------+----------------+
我如何检查 table table items_mapping 中是否有重复 device_uid 的客户?
简而言之,这会起作用:
+-------+--------------+------------+---------+
| uid | customer_uid | device_uid | offered |
+-------+--------------+------------+---------+
| 1132 | 53442 | 24 | 2.00 |
| 8242 | 53442 | 45 | 4.00 |
| 15122 | 53442 | 12 | 5.00 |
+-------+--------------+------------+---------+
但不应该是:
+-------+--------------+------------+---------+
| uid | customer_uid | device_uid | offered |
+-------+--------------+------------+---------+
| 1132 | 53442 | 24 | 2.00 |
| 8242 | 53442 | 45 | 4.00 |
| 15122 | 53442 | 24 | 2.00 |
+-------+--------------+------------+---------+
只需按 customer_uid
和 device_uid
以及 having
子句分组:
SELECT
customer_uid,
device_uid,
COUNT(device_uid) count_device_uid
FROM
items_mapping
GROUP BY
customer_uid,
device_uid
HAVING
COUNT(device_uid) > 1
这将为您提供重复 device_uid
的所有客户
不确定它是否运行,但这应该会给出您的结果:
SELECT
COUNT(*),
customer_id,
device_uid,
FROM
items_mapping
GROUP BY
device_uid, customer_id
HAVING COUNT(*) > 1
我有两个 table 一对多关系。我想检查映射 table.
中是否有任何重复条目客户:
+----------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------------+--------------+------+-----+---------+----------------+
| uid | int(11) | NO | PRI | | auto_increment |
| active | tinyint(1) | NO | | | |
| locked | tinyint(1) | NO | | | |
+----------------------------+--------------+------+-----+---------+----------------+
items_mapping:
+--------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+----------------+
| uid | int(11) | NO | PRI | | auto_increment |
| customer_uid | int(11) | NO | | | |
| device_uid | int(11) | NO | | | |
| offered | float(11,2) | NO | | | |
+--------------+---------------+------+-----+---------+----------------+
我如何检查 table table items_mapping 中是否有重复 device_uid 的客户?
简而言之,这会起作用:
+-------+--------------+------------+---------+
| uid | customer_uid | device_uid | offered |
+-------+--------------+------------+---------+
| 1132 | 53442 | 24 | 2.00 |
| 8242 | 53442 | 45 | 4.00 |
| 15122 | 53442 | 12 | 5.00 |
+-------+--------------+------------+---------+
但不应该是:
+-------+--------------+------------+---------+
| uid | customer_uid | device_uid | offered |
+-------+--------------+------------+---------+
| 1132 | 53442 | 24 | 2.00 |
| 8242 | 53442 | 45 | 4.00 |
| 15122 | 53442 | 24 | 2.00 |
+-------+--------------+------------+---------+
只需按 customer_uid
和 device_uid
以及 having
子句分组:
SELECT
customer_uid,
device_uid,
COUNT(device_uid) count_device_uid
FROM
items_mapping
GROUP BY
customer_uid,
device_uid
HAVING
COUNT(device_uid) > 1
这将为您提供重复 device_uid
不确定它是否运行,但这应该会给出您的结果:
SELECT
COUNT(*),
customer_id,
device_uid,
FROM
items_mapping
GROUP BY
device_uid, customer_id
HAVING COUNT(*) > 1