如何编写查询来检索公共数据

发布于 2025-01-07 07:16:59 字数 727 浏览 2 评论 0原文

可能的重复:
mysql中的相交

我做了一个用于检索公共数据的php项目。我在编写MySQL查询方面有点弱。我尝试了不同的方法。但我无法获得正确的输出

我的表是

 Id  Product-1   product-2
 -------------------------
 1      A           B
 2      B           C
 3      C           A
 4      E           B
 5      H           C

这是我的表。我想编写一个 MySQL 查询检索常见元素。 例如: 这里A,B将是Product-1,Product-2, 我想检索A,B的公共元素元素 这里输出是 C

    3      C           A
    2      B           C

两行都可以包含 Product-1 或 Product-2 中的 A,B

与 A,c 相同,公共元素是 B 。如何为此编写查询...在 sql 中我们可以使用 Intersect 操作 但 MySQL 我不知道。请帮助我......

Possible Duplicate:
Intersect in mysql

I have do a php project for retrieving common data.I am little weak in writing MySQL query. i was tried different ways .But i cannot get proper output

My table is

 Id  Product-1   product-2
 -------------------------
 1      A           B
 2      B           C
 3      C           A
 4      E           B
 5      H           C

This is my table .I want to write a MySQL query retrieve common elements.
eg:
Here A,B be will comes to Product-1,Product-2,
I want to retrieve common element element of A,B
Here The output is C

    3      C           A
    2      B           C

both row can contain either A,B in Product-1 or product-2

Same like A,c common element is B .How to write Query for this... in sql we can use Intersect operation
But MySQL i don't have any idea .Please help me...

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

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

发布评论

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

评论(1

老旧海报 2025-01-14 07:16:59

我建议不要将列用于双向关系,这会使查询复杂化,而应保持一种关系。

因此,而不是以下含义 A 到 CC 到 A

Id  Product-1   Product-2
 -------------------------
 3      C           A

您实际上这样做是为了表示 A 到 CC到 A

Id  Product-1   Product-2
 -------------------------
 2      A           C
 3      C           A

就是这样:

SELECT Product-1
FROM tablename
WHERE Product-2 IN ('A', 'B')
GROUP BY Product-1
HAVING COUNT(*) = 2

但是,根据您的数据,以下查询将给出 'A'、'B' 的公共元素:

SELECT Product-1 FROM
((SELECT Product-1, Product-2
 FROM tablename)
UNION
(SELECT Product-2, Product-1
 FROM tablename)) t
WHERE Product-2 IN ('A', 'B')
GROUP BY Product-1
HAVING COUNT(*) = 2

然后,假设没有重复项,您的查询 我所做的是创建一个派生表,其中包含反转关系的副本,这样我就能使关系成为一种方式,然后正常进行。

What I would recommend, instead of using the columns for two way relationships, which complicates the query, keep the relationships one way.

So, instead of the following meaning A to C and C to A:

Id  Product-1   Product-2
 -------------------------
 3      C           A

You actually do this to mean A to C and C to A:

Id  Product-1   Product-2
 -------------------------
 2      A           C
 3      C           A

Then, assuming no duplicates, your query is simply this:

SELECT Product-1
FROM tablename
WHERE Product-2 IN ('A', 'B')
GROUP BY Product-1
HAVING COUNT(*) = 2

However, with your data, the following query will give the common elements for 'A', 'B':

SELECT Product-1 FROM
((SELECT Product-1, Product-2
 FROM tablename)
UNION
(SELECT Product-2, Product-1
 FROM tablename)) t
WHERE Product-2 IN ('A', 'B')
GROUP BY Product-1
HAVING COUNT(*) = 2

What I've done is create a derived table that has a copy of the relationships reversed, so that I get the relationships to be one way, and then proceed normally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文