无法从SQL Server表中检索值

时间:2016-07-11 04:46:53

标签: sql-server information-retrieval

我正在尝试从表中检索数据。但我遇到了麻烦。

我有桌子:

所有者

Owners

患者

Patient

推荐

referral

治疗

treatments

医院

hospitals

Hospitals_treatments

hospitals_treatments

如何为已被转介到伯恩维尔动物医院的患者选择所有者详细信息?任何人都可以为我提供解决方案吗?

我尝试了以下查询,但它将所有所有者多次返回160.我似乎没有工作。

select 
    isnull(o.title, ' ') + ' ' + isnull(o.first_name, ' ') + ' ' + 
    isnull(o.last_name, ' ') as Owner, 
    isnull(o.address, ' ') as Address   
from 
    Owners o, appointment_details ad, referral r,treatments t, hospitals_treatments ht, hospitals h,patients p
where 
    r.treatmentstreatment_id = (select TOP 1 ht.treatmentstreatment_id 
                                from hospitals_treatments ht 
                                where ht.hospitalshospital_id = (select hospital_id  
                                                                 from hospitals 
                                                                  where name='Bourneville Animal Hospital Middlesex')) 
  and r.appointment_detailsappointment_id = ad.appointment_id;

表格查询:

CREATE TABLE Owners 
(
  owner_id   INT NOT NULL IDENTITY, 
  title      varchar(255) NULL, 
  first_name varchar(255) NULL, 
  last_name  varchar(255) NULL, 
  address    varchar(255) NULL, 
  PRIMARY KEY (owner_id)
);

CREATE TABLE appointment_details 
(
  appointment_id     INT NOT NULL IDENTITY, 
  appointment_date   datetime NULL, 
  details            text NULL, 
  patientspatient_id INT NOT NULL, 
  vetsvet_id         INT NOT NULL, 
  cost               varchar(255) NULL, 
  PRIMARY KEY (appointment_id)
);

CREATE TABLE referral 
(
   referral_id                       INT IDENTITY NOT NULL, 
   sessions                          int NULL, 
   appointment_detailsappointment_id INT NOT NULL, 
   treatmentstreatment_id            INT NOT NULL, 
   PRIMARY KEY (referral_id)
);

CREATE TABLE treatments 
(
    treatment_id INT identity NOT NULL, 
    name         varchar(255) NULL, 
    PRIMARY KEY (treatment_id)
);

CREATE TABLE hospitals 
(
    hospital_id INT IDENTITY NOT NULL, 
    name        varchar(255) NULL, 
    PRIMARY KEY (hospital_id)
);

CREATE TABLE hospitals_treatments 
(
  hospitalshospital_id   INT  NOT NULL, 
  treatmentstreatment_id INT NOT NULL, 
  PRIMARY KEY (hospitalshospital_id, treatmentstreatment_id)
);

ALTER TABLE patients 
ADD CONSTRAINT FKpatients296497 
    FOREIGN KEY (Ownersowner_id) 
    REFERENCES Owners (owner_id);

ALTER TABLE referral 
ADD CONSTRAINT FKreferral97180 
    FOREIGN KEY (appointment_detailsappointment_id)
    REFERENCES appointment_details (appointment_id);

ALTER TABLE hospitals_treatments 
ADD CONSTRAINT FKhospitals_169422 
    FOREIGN KEY (hospitalshospital_id)
    REFERENCES hospitals (hospital_id);

ALTER TABLE hospitals_treatments 
ADD CONSTRAINT FKhospitals_718862 
    FOREIGN KEY (treatmentstreatment_id) 
    REFERENCES treatments (treatment_id);

1 个答案:

答案 0 :(得分:0)

根据您提供的表格结构,尝试此操作,它会向所有者提供您正在寻找的信息。

select DISTINCT
    isnull(o.title, ' ') + ' ' + isnull(o.first_name, ' ') + ' ' + 
    isnull(o.last_name, ' ') as Owner, 
    isnull(o.address, ' ') as Address   
from 
    Owners o
    inner join patients p on o.owner_id = p.Ownersowner_id  
    inner join appointment_details ad on ad.patientspatient_id = p.patient_id
    inner join referral r on r.appointment_detailsappointment_id = ad.appointment_id
where exists (  select 1
                from hospitals_treatments ht 
                inner join hospitals h ON h.hospital_id = ht.hospitalshospital_id
                where h.name='Bourneville Animal Hospital Middlesex'
                    AND ht.treatmentstreatment_id = r.treatmentstreatment_id 
            )