如何编写查询来检索公共数据
可能的重复:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议不要将列用于双向关系,这会使查询复杂化,而应保持一种关系。
因此,而不是以下含义
A 到 C
和C 到 A
:您实际上这样做是为了表示
A 到 C
和C到 A
:就是这样:
但是,根据您的数据,以下查询将给出
'A'、'B'
的公共元素:然后,假设没有重复项,您的查询 我所做的是创建一个派生表,其中包含反转关系的副本,这样我就能使关系成为一种方式,然后正常进行。
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
andC to A
:You actually do this to mean
A to C
andC to A
:Then, assuming no duplicates, your query is simply this:
However, with your data, the following query will give the common elements for
'A', 'B'
: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.