SQL 选择具有(部分)重复数据的行

发布于 2024-10-07 00:04:51 字数 582 浏览 11 评论 0原文

我有以下数据库模式:

Product ID | Component | ...

产品 ID - 外键

组件 - 产品的一部分

由于某些神秘的原因,许多记录具有相同的产品 ID 和产品 ID。成分。是否有一个 SQL 查询可以返回所有产品 ID 和产品 ID?具有多个相同组件的组件?

例如,给出下表

| Product ID | Component |
--------------------------
| 1          | c1000     |
| 1          | c1100     |
| 2          | c2000     |
| 2          | c2000     |
| 2          | c2200     |
| 3          | c3000     |

SQL 查询应返回:

| Product ID | Component |
--------------------------
| 2          | c2000     |

I have the following database schema:

Product ID | Component | ...

Product ID - a foreign key

Component - parts of the Product

For some Arcane reason a number of Records have the same Product ID & Component. Is there a SQL query that would return all the Product ID's & Components, that have multiple identical Components?

E.g. given the following table

| Product ID | Component |
--------------------------
| 1          | c1000     |
| 1          | c1100     |
| 2          | c2000     |
| 2          | c2000     |
| 2          | c2200     |
| 3          | c3000     |

The SQL query should return:

| Product ID | Component |
--------------------------
| 2          | c2000     |

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

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

发布评论

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

评论(3

怀里藏娇 2024-10-14 00:04:51
SELECT
  ProductId,
  Component
FROM
  Table
GROUP BY
  ProductId,
  Component
HAVING
  COUNT(*) > 1
SELECT
  ProductId,
  Component
FROM
  Table
GROUP BY
  ProductId,
  Component
HAVING
  COUNT(*) > 1
半寸时光 2024-10-14 00:04:51
SELECT ProductId, Component, count(*) Duplicates
 from MyTable  --  or whatever
 group by ProductId, Component
 having count(*) > 1

这还将显示有多少重复条目。

SELECT ProductId, Component, count(*) Duplicates
 from MyTable  --  or whatever
 group by ProductId, Component
 having count(*) > 1

This will also show you how many duplicate entries there are.

晨曦慕雪 2024-10-14 00:04:51
select "Product ID", Component
from table
group by "Product ID", Component
having count(*) > 1
select "Product ID", Component
from table
group by "Product ID", Component
having count(*) > 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文