-4

我在数据库中有四个表。

  • 人(Perid,名字,姓氏,性别)
  • 研究(Resid,Resname,stareddate)
  • person_research(Resid , Perid , 其他)
  • 主管(Resid,Supervisorid,其他)

我想编写返回每个研究和研究人员姓名和主管姓名的存储过程,以便将结果放在 datagridview 中。

4

2 回答 2

1

您没有指定 DBMS,所以我假设 PostgreSQL:

create or replace function get_researchers()
  returns table(research_name, researchers, supervisors)
  language sql
AS
$body$
  select r.resname as research_name,
         string_agg(p.firstname||' '||p.lastname, ',') as researchers,
         string_agg(sp.firstname||' '||sp.lastname, ',') as supervisors
  from research r
    join person_research pr on pr.resid = r.resid
    join person p on pr.perid = p.perid,
    join supervisors s on s.resid = r.resid
    join person sp on sp.perid = s.perid
  group by r.resname
  order by r.resname;  
$body$
于 2012-04-05T14:11:17.400 回答
0

对于 SQL 服务器:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE dbo.test 
AS
BEGIN

    SET NOCOUNT ON;

    SELECT R.resname as researchname
        , (p1.firstname + p1.lastname) as researcher
        , (p2.firstname + p2.lastname) as supervisor
    FROM Research R
    JOIN person_research PR
        ON R.Resid = PR.Resid
    JOIN person p1
        on PR.Perid = p1.Perid 
    JOIN supervisors s
        on R.Resid = s.Resid
    JOIN person p2
        on PR.Perid = p2.Perid 
END
GO
于 2012-04-05T14:15:20.570 回答