我正在尝试使用子查询从表中获取数据,但是我收到了这个错误:
基准表:[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 drugname获取数据,数量和销售建议我,该怎么做
答案 0 :(得分:0)
你只需要加入而不是凌乱的子查询。
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
答案 1 :(得分:0)
JOIN是要走的路,但要注意INNER JOIN,除非你确定在每种情况下总是有匹配的记录 - 如果没有,那么使用LEFT JOIN s。如果你确定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。