8

Sql 小提琴

CREATE TABLE [Users_Reg]
(
   [User_ID]        [int] IDENTITY (1, 1) NOT NULL CONSTRAINT User_Reg_P_KEY PRIMARY KEY,
   [Name]           [varchar] (50) NOT NULL, 
   [Type]           [varchar] (50) NOT NULL /*Technician/Radiologist*/
)

CREATE Table [Study]
(
  [UID]             [INT] IDENTITY (1,1) NOT NULL CONSTRAINT Patient_Study_P_KEY PRIMARY KEY,
  [Radiologist]     [int], /*user id of Radiologist type*/
  [Technician]      [int], /*user id of Technician type*/

)


select * from Study
inner join Users_Reg
on Users_Reg.User_ID=Study.Radiologist

在 patient_study 表中,Radiologist 或 Technician 的值可能为 0。我如何从查询中获取技术人员姓名和放射科医生姓名。

4

5 回答 5

7

您将需要在users_reg表上加入两次以获得结果。一次用于放射科医生,另一次用于技术人员:

select ps.uid,
  ur1.name rad_name,
  ur1.type rad_type,
  ur2.name tech_name,
  ur2.type tech_type
from Patient_Study ps
left join Users_Reg ur1
  on ur1.User_ID=ps.Radiologist
left join Users_Reg ur2
  on ur2.User_ID=ps.Technician;

请参阅SQL Fiddle with Demo。这将返回所有患者研究的放射科医生和技术人员姓名/类型。如果要替换任何列中的 null,则可以使用COALESCE类似于以下内容:

select ps.uid,
  coalesce(ur1.name, '') rad_name,
  coalesce(ur1.type, '') rad_type,
  coalesce(ur2.name, '') tech_name,
  coalesce(ur2.type, '') tech_type
from Patient_Study ps
left join Users_Reg ur1
  on ur1.User_ID=ps.Radiologist
left join Users_Reg ur2
  on ur2.User_ID=ps.Technician;

请参阅带有演示的 SQL Fiddle

于 2013-08-02T10:48:13.673 回答
3

在您的情况下,您可以使用isnull

select * 
from Patient_Study as PS
    inner join Users_Reg as U on U.User_ID = isnull(nullif(PS.Radiologist, ''), PS.Technician)

或两个left outer joins

select PS.*, isnull(UP.Type, UT.Type) as Type
from Patient_Study as PS
  left outer join Users_Reg as UP on UP.User_ID = PS.Radiologist
  left outer join Users_Reg as UT on UT.User_ID = PS.Technician;

SQL FIDDLE示例。

作为建议,我可能会说 -null如果您没有适当的价值,请将值存储在您的表中

INSERT INTO Patient_Study(Radiologist,Technician)VALUES('1','2')
INSERT INTO Patient_Study(Radiologist,Technician)VALUES('1','')
INSERT INTO Patient_Study(Radiologist,Technician)VALUES(null,'2')
于 2013-08-02T10:47:23.407 回答
1

检查这个小提琴

编码:

SELECT   * 
FROM     Patient_Study
INNER JOIN Users_Reg
         ON CASE 
                WHEN Patient_Study.Radiologist = '' 
                THEN Patient_Study.Technician 
                ELSE Patient_Study.Radiologist 
            END = Users_Reg.User_ID;

如果您将其设为 NULL 而不是 "" (我更喜欢)

SELECT   * 
FROM     Patient_Study
INNER JOIN Users_Reg
         ON COALESCE(Patient_Study.Radiologist,Patient_Study.Technician) = Users_Reg.User_ID

我选择 COALESCE 而不是 ISNULL,因为您可以在其中添加其他技术人员。

于 2013-08-02T10:45:58.610 回答
1

这个怎么样。

select *,'Radiologist' as StudiedBy from Patient_Study
inner join Users_Reg
on Users_Reg.User_ID=Patient_Study.Radiologist
and Patient_Study.Radiologist>0
Union
select *,'Technician' from Patient_Study
inner join Users_Reg
on Users_Reg.User_ID=Patient_Study.Technician 
and Patient_Study.Technician >0

我添加了一个标记 StudiedBy 以区分放射科医师和技术员。您可以添加 where 条件来检查特定的 User_Reg

于 2013-08-02T10:51:15.897 回答
1
select R.Name as TechnicianName,T.Name as RadiologistName 
from Patient_Study
inner join Users_Reg R
on Patient_Study.UID=R.User_ID

inner join Users_Reg T
on Patient_Study.UID=T.User_ID.

http://sqlfiddle.com/#!3/25d42/41

于 2013-08-02T10:51:24.600 回答