我在MS SQL SERVER的数据库中拥有3个不同的表。
该 LAB Patientreg_ 和 Patient_master 是两个包含患者详细信息的表(两个表中的患者都不相同, LAB Patientreg_ 是与直接在实验室中注册的患者有关的表,而 Patient_master 是关于在 门诊 或接待时 在 医院中注册的 患者 的表)和 patient_address 是包含在表中登记的患者的地址表 patient_master 。
在某些情况下,我需要所有患者的数据,包括 LAB Patientreg_ 和 Patient_master 以及 Patient_address 。我的目的是创建一个将表中所有值放在一起的视图(即,如果 Patient_master 有50行,而 LAB Patientreg_ 有32行,则结果视图应为82行),并且列的数量和类型也仅限于 LAB Patientreg_ 但它不起作用,是否可以像我说的那样创建视图?如果不是,我应该怎么做才能获得这种观点?在此先感谢您的帮助。
表的结构如下,
Patient_master
CREATE TABLE [dbo].[patient_master]( [pid] [int] IDENTITY(1,1) NOT NULL, [date_created] [date] NULL, [title] [varchar](25) NULL, [fname] [varchar](255) NULL, [mname] [varchar](255) NULL, [lname] [varchar](255) NULL, [addr_id] [int] NULL, [sex] [char](1) NULL, [dob] [date] NULL, [dobType] [tinyint] NULL, [marital_status] [int] NULL, [religion] [int] NULL, [caste] [int] NULL, [aadhar_no] [varchar](16) NULL, [mother_name] [varchar](100) NULL, [guardian_name] [varchar](100) NULL, [guardian_rltn] [varchar](100) NULL, [allow_sms] [varchar](10) NULL, [allow_email] [varchar](10) NULL, [occupation] [int] NULL, [monthly_income] [int] NULL, [financial_review_date] [datetime] NULL, [race] [int] NULL, [language] [int] NULL, [referrer] [varchar](100) NULL, [referrer_id] [varchar](100) NULL, [interpretter] [varchar](100) NULL, [interpretter_id] [varchar](100) NULL, [deceased_date] [date] NULL, [deceased_reason] [varchar](255) NULL, [created_by] [varchar](100) NULL, [changed_by] [varchar](100) NULL, [date_changed] [datetime] NULL, [voided] [tinyint] NULL, [voided_by] [varchar](100) NULL, [date_voided] [datetime] NULL, [void_reason] [varchar](255) NULL, [block] [tinyint] NULL, [aliasname] [varchar](50) NULL, [confidential] [int] NULL, [hospno] [char](8) NULL, PRIMARY KEY(pid) );
Patient_address
CREATE TABLE [dbo].[patient_address]( [pres_house_no] [varchar](25) NULL, [pres_city] [varchar](50) NULL, [pres_street] [varchar](50) NULL, [pres_district] [int] NULL, [pres_pincode] [varchar](10) NULL, [perm_house_no] [varchar](25) NULL, [perm_city] [varchar](50) NULL, [perm_street] [varchar](50) NULL, [perm_district] [int] NULL, [perm_pincode] [varchar](10) NULL, [home_phone] [varchar](11) NULL, [mobile_phone] [varchar](11) NULL, [other_phone] [varchar](11) NULL, [work_phone] [varchar](11) NULL, [email] [varchar](255) NULL, [emerg_cntct] [varchar](50) NULL, [emerg_cntct_rltn] [varchar](50) NULL, [emerg_cntct_num] [varchar](11) NULL, [pres_place] [varchar](255) NULL, [perm_place] [varchar](255) NULL, [addr_id] [int] IDENTITY(1,1) NOT NULL, [pid] [int] NULL, [active] [char](1) NULL, [phone_type] [varchar](4) NULL, , PRIMARY KEY(addr_id) );
LAB_patientreg
CREATE TABLE [dbo].[LAB_patientreg]( [id] [int] IDENTITY(1,1) NOT NULL, [patientid] [varchar](15) NOT NULL, [title] [varchar](15) NULL, [fname] [varchar](50) NULL, [mname] [varchar](50) NULL, [lname] [varchar](50) NULL, [dob] [smalldatetime] NULL, [gender] [char](2) NULL, [mstatus] [varchar](15) NULL, [idtype] [varchar](15) NULL, [idno] [varchar](25) NULL, [mtongue] [varchar](35) NULL, [address] [varchar](300) NULL, [phtype] [varchar](15) NULL, [phno] [varchar](20) NULL, [emailid] [varchar](50) NULL, [smsstatus] [int] NULL, [mailstatus] [int] NULL, [status] [int] NULL, [regcenter] [varchar](30) NULL, [regdate] [smalldatetime] NULL, PRIMARY KEY(patientid) );
我尝试了以下
Select patientid,title, fname, mname, lname, NULL As religion from LAB_patientreg WHERE 1=1 Union All Select pid,title, fname, mname, lname, religion from patient INNER JOIN patient_address ON patient.pid = patient_address.pid WHERE 1=1
执行此操作时,将发生错误,错误代码为209,SQL状态为S1000:列名“ pid”不明确”
您可能要在这里选择联盟?
在一个视图中包含两个表数据,并且能够向第二个表添加条件。就像这样:
Select title, fname, mname, lname, NULL As religion, patientid As pid from dbo.LAB_patientreg WHERE 1=1 UNION Select title, fname, mname, lname, religion, convert(varchar, patient_master.pid) As pid from dbo.patient_master INNER JOIN patient_address ON patient_master.pid = patient_address.pid WHERE 1=1
(只需用您的条件替换where 1 = 1)
使用“联合选择”:确保 两个 表上的列名称相同。如果您在另一个表中没有该字段,则可以使用AlternateFieldname As TheFieldNameINeed“例如”Null As religion
AlternateFieldname As TheFieldNameINeed
Null As religion
编辑:您可以Union用来获取所有不同的值或Union All获取两个表的所有值。
Union
Union All