表连接在哪里

时间:2009-11-26 08:04:50

标签: sql oracle join

表1 t10(id)

id
---
1
2

表t11(a1,a2,a3)

a1  a2  a3
----------
1   10   a
1   10   b
1   11   b
1   12   c
2   20   d
2   21   e

从t10 a,t11 b中选择*,其中a.id = b.a1 如何显示

id  a1  a2  a3
--------------
1   1   10  a
1   1   10  b //(not display this row)
1   1   11  b //(not display this row)
1   1   12  c //(not display this row) 
2   2   20  d
2   2   21  e //(not display this row)

得到t11的随机行

maybe display this
id  a1  a2 
---------- 
1   1   11  b
1   1   10  a //(not display this row)
1   1   10  b //(not display this row)
1   1   12  c //(not display this row) 
2   2   20 
2   2   21  //(not display this row) 

3 个答案:

答案 0 :(得分:3)

select a1 as id, a1, min(a2) as a2
from t11
group by a1

会给你:

id  a1  a2
----------
1   1   10
2   2   20

答案 1 :(得分:0)

这就是答案:

SELECT *
  FROM t10 a, (
       SELECT * FROM (
           SELECT b.*, ROW_NUMBER() OVER(PARTITION BY a10 ORDER BY a10) as rn
           FROM t11 b
       ) WHERE rn =1) b
 WHERE a.id = b.a10(+)

答案 2 :(得分:0)

这看起来几乎就像他想要从ms访问一样的FIRST / LAST。

这可以使用

在Sql Server中完成(非常密切)
DECLARE @Table TABLE(
        id INT,
        a1 INT,
        a2 INT
)

INSERT INTO @Table (id,a1,a2) SELECT 1, 1, 11
INSERT INTO @Table (id,a1,a2) SELECT 1, 1, 10
INSERT INTO @Table (id,a1,a2) SELECT 1, 1, 12
INSERT INTO @Table (id,a1,a2) SELECT 2, 2, 20
INSERT INTO @Table (id,a1,a2) SELECT 2, 2, 21

SELECT  *
FROM    @Table t
WHERE   a2 = (SELECT TOP 1 a2 FROM @Table WHERE id = t.id AND a1 = t.a1)