对具有多个匹配行的表进行联接的 SQL 查询
编辑:我最初的描述不是很清楚,所以让我重新开始:
我有一个表 x,其中包含 a、b 和 c 列,我想查询表 y 中的内容,其中 ya = xa AND 在表 x 中有一行 xb = (arg1) AND xc = (arg2) 并且 x 中存在另一行,其中 xb = (arg3) 且 xc = (arg4)
因此在其中传递值 (3, 4, 6, 12) 会在此测试用例中返回 a1。希望事情能澄清。
|-------|----------|----------|
| a | b | c |
|-------|----------|----------|
| 1 | 3 | 4 |
| 1 | 6 | 12 |
| 2 | 6 | 33 |
EDIT: My initial description wasn't a very clear one, so let me start over:
I have one table x which has columns a, b, and c and I want to query content from table y where y.a = x.a AND in table x there is a row where x.b = (arg1) AND x.c = (arg2) AND another row exists in x where x.b = (arg3) and x.c = (arg4)
So passing in values (3, 4, 6, 12) in there would return a1 in this test case. Hope that clears things up.
|-------|----------|----------|
| a | b | c |
|-------|----------|----------|
| 1 | 3 | 4 |
| 1 | 6 | 12 |
| 2 | 6 | 33 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这听起来像是父级 -> 父级之间的基本一对多 (1:M) 连接。子表关系。如果是这样,基本的 INNER JOIN 将返回父表和子表之间的所有匹配行。父列(如果包含在 SELECT 子句中)将为子表中的每个匹配行重复。
父级和子级之间的 LEFT OUTER JOIN 将返回父级的所有行和子级的匹配行。如果子表中缺少 ID 值,通常会返回 NULL 值来代替任何数据。
希望这有帮助。
This sounds like a basic 1-to-Many (1:M) join between a parent -> child table relationship. If so, a basic INNER JOIN would return all matching rows between the parent and child table. The parent columns (if included in the SELECT clause) will repeat for each matching row in the child table.
A LEFT OUTER JOIN between the parent and the child would return all rows from the parent and matching rows from the child. If the ID value is missing from the child table a NULL value is commonly returned in place of any data.
Hope this helps.
左连接...?
您要问的问题似乎很简单 - 您能否提供一些详细信息(表架构等),以便我们提供更多帮助?
Left Join...?
What you're asking seems really simple - can you provide some details (table schemas etc) so we can be more helpful?