我目前是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 |
+------+-------+------------+-----------+
我正在尝试创建一个新表,其中包含staff 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 |
+---------+---------------+
我做错了什么?
提前致谢。
答案 0 :(得分: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
答案 1 :(得分:0)
比较Staff表和research表之间的S_ID数据类型。任何一个表中都可能有空格。否则,请在连接条件中使用Trim
函数,以便可以收到正确的结果集。
您的查询仅返回Staff表中的不同ID。您应该在SQL语句中使用group by
。
希望这有帮助!