【问题标题】:Cannot retrieve value from SQL Server table无法从 SQL Server 表中检索值
【发布时间】:2016-07-11 14:24:17
【问题描述】:

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

我有桌子:

所有者

患者

推荐

治疗

医院

医院治疗

如何选择已转诊至伯恩维尔动物医院的患者的所有者详细信息?谁能为此提供解决方案?

我尝试使用以下查询,但它多次将所有所有者返回到 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);

【问题讨论】:

  • 你自己有没有尝试过?
  • 是的,尝试了以下方法:
  • 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;
  • 它似乎不起作用
  • Bad habits to kick : using old-style JOINs - 旧式 逗号分隔的表格列表 样式已替换为 ANSI 中的 proper ANSI JOIN 语法-92 SQL 标准(20 多年前),不鼓励使用它

标签: sql-server information-retrieval


【解决方案1】:

根据您提供的表结构,试试这个,它会提供您正在寻找的所有者信息。

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 
            ) 

【讨论】:

    猜你喜欢
    • 2014-08-22
    • 2011-06-28
    • 2013-06-27
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    相关资源
    最近更新 更多