-1

我有两个表:

员工

emp_id, emp_name, emp_dept, emp_sal

1         A           A         1

2         B           B         2

3         C           C         3

4         D           D         4

审判

emp_id, random

1          X

1          Y

2          Z

3          A

4          B

如果我要执行 Employee 和 Trial 的 Natural join,

我会得到结果:

emp_id, emp_name, emp_dept, emp_sal random

1         A           A         1      X

1         A           A         1      Y

2         B           B         2      Z

3         C           C         3      A

4         D           D         4      B

如果我想执行上述自然联接并在单个查询中计算组织中的部门数,我将如何去做?

select count(emp_dept) from (select distinct(emp_dept) from employee natural join trial) as T2;

有效,但我想知道是否还有另一种方法可以做到以上几点?

笔记:

自然加入是必须的!

我尝试了以下两个代码,但都失败了。:P

  1. select count(emp_dept) from (select (emp_dept) from employee natural join trial) as T2; 当实际答案是 4 时,给了我 5。

  2. select count(select distinct(emp_dept) as emp_date from (select * from employee natural join trial) as T2) from T2;

  3. select count(select distinct(emp_dept) as emp_date from (select * from employee natural join trial) as T2) from (select * from employee natural join trial) as T3);

^ 我在上面的 3 行代码中做错了什么?

提前致谢!:)

4

1 回答 1

0

你不能只做一个 COUNT(DISTINCT .... ) 吗?像这样:-

SELECT COUNT(DISTINCT emp_dept) 
FROM employee 
NATURAL JOIN trial
于 2013-05-02T09:15:10.410 回答