0

例如,我们有这 2 个表

表格1:

Dir_num |  dir_name
10      | john
11      | vlad

表2:

game    |  dir_num
101     | 10
111     | 10
102     | 11
104     | 10
104     | 10

现在我尝试找到玩了不到 2 场比赛的 dir_name。

select * from table1 where dir_num  ....

请帮我找到 pl/sql 代码。

输出应显示

 Vlad.
4

2 回答 2

2

尝试这样的事情 - 使用GROUP BYand HAVING

SELECT t.dir_num, t.dir_name
FROM Table1 t
  LEFT JOIN Table2 t2 ON t.dir_num = t2.dir_num
GROUP BY t.dir_num, t.dir_name
HAVING COUNT(DISTINCT t2.game) < 2

这是SQL Fiddle

如果您想要两个表中的那些(意味着至少 1 个游戏),则将 LEFT JOIN 更改为 INNER JOIN。

祝你好运。

于 2013-01-31T04:22:35.250 回答
1

请试试:

SELECT t.dir_num, t.dir_name
FROM Table1 t
  INNER JOIN Table2 t2 ON t.dir_num = t2.dir_num
GROUP BY t.dir_num, t.dir_name
HAVING COUNT(DISTINCT t2.game) < 2
于 2013-01-31T04:52:25.513 回答