【问题标题】:How to get data from table using sub query in sql server 2008?如何在 sql server 2008 中使用子查询从表中获取数据?
【发布时间】:2016-10-24 06:37:56
【问题描述】:

我正在尝试使用子查询从表中获取数据,但出现此错误:

基表:[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 药物名称、数量和销售建议中获取数据,该怎么做

【问题讨论】:

    标签: c# asp.net sql-server database sql-server-2008


    【解决方案1】:

    你只需要加入而不是凌乱的子查询。

      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
    

    【讨论】:

      【解决方案2】:

      JOIN 是要走的路,但要注意 INNER JOIN,除非您确定在每种情况下总是有匹配的记录 - 如果没有,请使用 LEFT JOINs。如果您确定 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。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-06
        • 1970-01-01
        • 2013-12-09
        相关资源
        最近更新 更多