0

我目前是 SQL 语言的新手,我正在努力理解如何从下表中检索正确的信息:

职员表

+------+----------+---------+-----------------+------+------+
| s_id | initials | s_name  | pos             | qual | d_id |
+------+----------+---------+-----------------+------+------+
| AA   | A.       | Tom     | Lecturer        | PhD  | RELI |
| BBB  | J.J.     | Jason   | Senior Lecturer | PhD  | RELI |
| CCC  | B.A.     | Andy    | Senior Lecturer | PhD  | BIOL |
| DD   | M.       | Mark    | Professor       | PhD  | BIOL |
| EE   | W.       | Alen    | Professor       | BA   | BIOL |
+------+----------+---------+-----------------+------+------+

研究表

+------+-------+------------+-----------+
| s_id | r_id  | str_date   | fin_date  |
+------+-------+------------+-----------+
| BBB  | RESH1 |       1990 |      1998 |
| BBB  | RESH2 |       1980 |      1985 |
| DD   | RESH4 |       1995 |      1999 |
| AA   | RESH4 |       1992 |      1999 |
| CCC  | RESH8 |       1989 |      1994 |
| EE   | RESH5 |       1988 |      1989 |
+------+-------+------------+-----------+

我正在尝试创建一个新表,其中将包含员工 s_name 以及每个人做了多少不同的研究。

到目前为止我所拥有的是

select distinct s.s_name, count(r.s_id) 
from staff s 
inner join research r 
on s.s_id = r.s_id 
where s.s_id = r.s_id;

我的结果是,

+---------+---------------+
| s_name  | count(r.s_id) |
+---------+---------------+
| Jason   |             6 |
+---------+---------------+

我做错了什么?

提前致谢。

4

2 回答 2

0

我认为这会做:

select s.s_name, count(*) 
from staff s 
inner join research r 
on s.s_id = r.s_id 
group by r.s_id
于 2012-11-08T12:16:16.670 回答
0

比较 Staff 表和 research 表之间 S_ID 的数据类型。任何一张表中都可能有空格。否则Trim,请在您的连接条件中使用该函数,以便获得正确的结果集。

您的查询仅返回 Staff 表中的不同 id。您应该group by在 SQL 语句中使用。

希望这可以帮助!

于 2012-11-08T12:30:17.567 回答