[英]How to get data from table using sub query in sql server 2008?
我正在尝试使用子查询从表中获取数据,但出现此错误:
基表:[tblPriscription]
[Priscriptionid] [bigint] IDENTITY(1,1) NOT NULL,
[patientId] [bigint] NULL,
[doctorId] [bigint] NULL,
[BillNo] AS ([Priscriptionid]+(100)),
[BillDate] [datetime] NULL,
[BillType] [nvarchar](50) NULL,
[PaymentBy] [nvarchar](50) NULL,
[DocumentType] [nvarchar](50) NULL,
[DocumentName] [nvarchar](50) NULL,
[bitIsActive] [bit] NULL,
[dateCreated] [date] NULL,
[bitIsDelete] [bit] NULL,
[bitisSave] [bit] NULL,
[TotalAmount] [decimal](18, 2) NULL,
我要从中获取数据的表:
dbo.tblPriscriptionDetail
[PriscriptionDtlid] [bigint] IDENTITY(1,1) NOT NULL,
[Priscriptionid] [bigint] NULL,
[drugId] [bigint] NULL,
[Rxno] [bigint] NULL,
[sigId] [bigint] NULL,
[Selling] [decimal](18, 2) NULL,
[Qty] [int] NULL,
[RefillQty] [int] NULL,
[RefillNo] [int] NULL,
[Days] [int] NULL,
[Amount] [decimal](18, 2) NULL,
[bitIsActive] [bit] NULL,
[dateCreated] [datetime] NULL,
[bitIsDelete] [bit] NULL,
[PurchaseDtlid] [bigint] NULL,
[bitIsSave] [bit] NULL,
[BillType] [nvarchar](50) NULL,
[PaymentBy] [nvarchar](50) NULL,
[patientId] [bigint] NULL,
[id] [bigint] NULL,
Qyery:
SELECT *,
(SELECT insuranceName
FROM tblinsurance
WHERE insuranceid = (SELECT insuranceid
FROM tblpatient
WHERE PatientId = tblPriscription.PatientId)) AS insuranceName,
(SELECT drfirstname + ' ' + drlastname
FROM tbldoctor
WHERE doctorid = tblPriscription.doctorid) AS doctorname,
(SELECT patFirstName + ' ' + patLastName
FROM tblPatient
WHERE patientid = tblPriscription.patientid) AS patname,
(SELECT policyno
FROM tblPatient
WHERE patientid = tblPriscription.patientid) AS policyno,
(SELECT insuranceplanname
FROM tblInsurancePlan
WHERE insuranceplanid = (SELECT insuranceplanid
FROM tblpatient
WHERE PatientId = tblPriscription.PatientId)) AS insurancePlanName,
(SELECT drugname
FROM tblDrugMaster
WHERE drugid = (SELECT drugid
FROM tblPriscriptionDetail
WHERE priscriptionid = tblPriscriptionDetail.priscriptionid)) AS drugname,
(SELECT qty
FROM tblPriscriptionDetail
WHERE priscriptionid = tblPriscriptionDetail.priscriptionid) AS qty,
(SELECT Selling
FROM tblPriscriptionDetail
WHERE priscriptionid = tblPriscriptionDetail.priscriptionid) AS selling
FROM tblPriscription
我正在尝试从tblPriscriptionDetail药名,数量和销售中获取数据,建议我如何做
您只需要加入而不凌乱的子查询。
SELECT tp.*, tpd.qty, tpd.Selling, ti.insuranceName
FROM tblPriscription tp inner join tblPriscriptionDetail tpd ON tpd.Priscriptionid = tp.Priscriptionid
inner join tblinsurance ti ON ti.insuranceid = tp.PatientId
inner join your other tables
JOIN是要走的路,但是要提防INNER JOIN,除非您确定每种情况下总是有匹配的记录-如果没有,则使用LEFT JOIN 。 如果您确定INNER JOIN不会丢失行,请尝试执行此操作
SELECT Selling, qty, drugname, insuranceplanname, policyno, patFirstName + ' ' + patLastName AS patname, drfirstname + ' ' + drlastname AS doctorname, insuranceName
FROM tblPrescription P
INNER JOIN tblPrescriptionDetail PD ON PD.PrescriptionID = P.PrescriptionID
INNER JOIN tblDrugMaster DM ON DM.PrescriptionID = PD.PrescriptionID
INNER JOIN tblPatient PP ON PP.PatientID = P.PatientID
INNER JOIN tblInsurancePlan IP ON IP.InsuranceplanID = P.InsuranceplanID
INNER JOIN tblDoctor D ON D.DoctorID = P.DoctorID
INNER JOIN tblInsurance I ON I.InsuranceID = P.InsuranceID
我试图在大小写上引入一些一致性,并且将您要提取的所有字段都包含在原始字段中(而不只是您想要的字段)。 我很好奇您同时拥有tblInsurance和tblInsurancePlan。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.