开发者

SQL Query for a join on a table with mulitple matching rows

开发者 https://www.devze.com 2023-04-11 21:53 出处:网络
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 th

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.

|-------|----------|----------|
| 开发者_JAVA百科  a   |     b    |     c    |
|-------|----------|----------|
|   1   |     3    |     4    |
|   1   |     6    |     12   |
|   2   |     6    |     33   |


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.


Select *
  from y
 where exists( select *
                 from x
                where y.a = x.a
                  and x.b = 3
                  and x.c = 4 )
   and exists( select *
                 from x
                where y.a = x.a
                  and x.b = 6
                  and x.c = 12 )


Left Join...?

What you're asking seems really simple - can you provide some details (table schemas etc) so we can be more helpful?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号