简体   繁体   English

SQL查询从三个不同的表中提取数据

[英]SQL query to pull data from three different tables

I have to create a SQL query to list all the Nurses in the 'Sparrow' Wing ordered by last name, first name. 我必须创建一个SQL查询,以按姓,名顺序列出“麻雀”翼中的所有护士。

However, I need to pull Nurse_name and Nurse_surname from the Nurse table, which is linked to another table called Sister by the foreign key Sister_ID, this table is then linked to another table called Wing which has the foreign key Sister_ID. 但是,我需要从Nurse表中拉出Nurse_name和Nurse_surname,该表通过外键Sister_ID链接到另一个名为Sister的表,然后将此表链接到另一个具有外键Sister_ID的表Wing。

The nurse is managed by the sister and the sister manages the wing. 护士由姐姐管理,而姐姐则负责管理。

Can anyone help me with an SQL query for this? 谁能帮助我进行SQL查询吗? As it is, I am only able to get the data from nurse and sister tables. 就这样,我只能从护士和姐妹表中获取数据。

Since you seem to be aware that you should use the inner join to connect tables (but apparently not that the connection needs to be via the related columns) you should apply that knowledge to connect all the tables you need to answer the query. 由于您似乎已经意识到应该使用inner join联接来连接表(但显然不需要通过相关列进行连接),因此您应该运用该知识来连接所有需要回答查询的表。

If you start at the end result and work your way backwards you first chose the columns you need: 如果您从最终结果开始并往回走,则首先选择所需的列:

select Nurse.Nurse_name, Nurse.Nurse_surname

and then as they belong to the Nurse table you use that as source in the from clause 然后由于它们属于Nurse表,因此在from子句中将其用作源

from Nurse

to get the Wing you need to connect the Sister table, but to connect that and Nurse you first need the SisterNurse table joined on the shared attribute 要获得Wing,您需要连接Sister表,但要连接它和Nurse,首先需要在shared属性上连接SisterNurse表

join SisterNurse on Nurse.Nurse_ID = SisterNurse.Nurse_ID

now you can join Sister on the attribute shared with SisterNurse 现在您可以在与SisterNurse共享的属性上加入Sister

join Sister on Sister.Sister_ID = SisterNurse.Sister_Id

and finally you can join Wing 最后你可以加入永安

join Wing on Wing.sister_ID = Sister.Sister_ID

limit the Wings to the one names 'Sparrow' 将机翼限制为“麻雀”

where Wing.Wing_Name = 'Sparrow'

and order the data 并排序数据

order by Nurse.Nurse_surname, Nurse.Nurse_name

Put it all together and you get: 放在一起,您将获得:

select Nurse.Nurse_name, Nurse.Nurse_surname
from Nurse
join SisterNurse on Nurse.Nurse_ID = SisterNurse.Nurse_ID
join Sister on Sister.Sister_ID = SisterNurse.Sister_Id
join Wing on Wing.sister_ID = Sister.Sister_ID
where Wing.Wing_Name = 'Sparrow'
order by Nurse.Nurse_surname, Nurse.Nurse_name

You don't give much information about the schema involved but maybe this will help: 您不会提供有关所涉及的架构的太多信息,但这可能会有所帮助:

select
  n.Nurse_name
, n.Nurse_surname
, w.Wing_name
, managing_nurse.Nurse_name
, managing_nurse.surname
from
  Nurse n
  join Sister s on n.Sister_ID=n.Sister_ID
  join Wing w on s.Sister_ID=w.Sister_ID
  join Nurse managing_nurse on w.Nurse_Manager_ID=managing_nurse.Sister_ID

A simple way is to use 'union': 一种简单的方法是使用“联合”:

select * from (
select nurse_name, nurse_surname, 'nurse' as nurse_type from nurse_table
   union 
select nurse_name, nurse_surname, 'sister' as nurse_type from syster join nurse_table on nurse_table.syster_id = syster.syster_id
   union 
select nurse_name, nurse_surname, 'wing' as nurse_type from wing join syster on wing.syster_id = syster.syster_id join nurse_table on nurse_table.syster_id =  syster.syster_id )
order by nurse_surname, nurse_name

Hope this help you! 希望这对您有所帮助!

PS I assume that syster and wing tables have nurse_name and nurse_surname fields. PS我假设syster表和wing表具有Nurse_name和Nurse_surname字段。 If not you have to give the same alias to all the selected columns 如果不是,则必须为所有选定的列赋予相同的别名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM