简体   繁体   English

SQL查询根据单独的查询结果联接多个表

[英]SQL query to join multiple tables based upon separate query result

I am trying to find the right SQL query to join multiple tables together based upon a separate query.. 我试图找到合适的SQL查询,以基于一个单独的查询将多个表连接在一起。

I query the 'Titles' table to get the 'TitleId' - I then need to query 'Titles_Hardware' and 'Titles_Software' to match the 'Title Id' to the 'Hardware ID'/'Software Id'. 我查询“标题”表以获取“ TitleId”-然后,我需要查询“ Titles_Hardware”和“ Titles_Software”,以将“ Title ID”与“ Hardware ID” /“ Software ID”进行匹配。

I then need to take those 'Hardware Ids' and 'Software Ids' to query the 'Hardware' and 'Software' tables to grab the friendly names of the Hardware and Software that particular user needs. 然后,我需要使用那些“硬件ID”和“软件ID”来查询“硬件”和“软件”表,以获取特定用户所需的硬件和软件的友好名称。

I have tried subqueries/cross join and many other queries I found on this site - none seem to give me what I need.. any suggestions would be absolutely amazing! 我尝试了子查询/交叉连接以及在此站点上发现的许多其他查询-似乎没有一个可以满足我的需求。.任何建议都绝对令人惊讶!

Outline of the database (tables are shown in italics).. 数据库的概要(表以斜体显示)。

在此处输入图片说明

This query should do what you want, but it's not clear what you mean by "join multiple tables together based upon a separate query..". 该查询应该执行您想要的操作,但是不清楚“根据一个单独的查询将多个表连接在一起。”的含义。 Here I just treated everything as tables. 在这里,我只是将所有内容都视为表格。

SELECT t.TitleName, t.TitleId, h.HardwareName, s.SoftwareName
FROM Titles t
INNER JOIN Title_Hardware th ON th.TitleId = t.TitleId
INNER JOIN Hardware h ON h.HardwareId = th.HardwareId
INNER JOIN Title_Software ts ON ts.TitleId = t.TitleId
INNER JOIN Software s ON s.SoftwareId = ts.SoftwareId
WHERE t.TitleId = 'bleh'

EDIT 编辑

From the comments, you need three queries to get what you want: 从注释中,您需要三个查询来获取所需的内容:

SELECT t.TitleName
FROM Titles t
WHERE t.TitleId = 'bleh'

SELECT h.HardwareName
FROM Titles t
INNER JOIN Title_Hardware th ON th.TitleId = t.TitleId
INNER JOIN Hardware h ON h.HardwareId = th.HardwareId
WHERE t.TitleId = 'bleh'

SELECT s.SoftwareName
FROM Titles t
INNER JOIN Title_Software ts ON ts.TitleId = t.TitleId
INNER JOIN Software s ON s.SoftwareId = ts.SoftwareId
WHERE t.TitleId = 'bleh'

It should just be a simple multi-level join 它应该只是一个简单的多级联接

SELECT TitleName, HardwareName, SoftwareName
FROM Titles
INNER JOIN Titles_Hardware
ON Titles.TitleId = Titles_Hardware.TitleId
INNER JOIN Titles_Software
ON Titles.TitleId = Titles_Software.TitleId
INNER JOIN Hardware
ON Hardware.HardwareId = Titles_Hardware.HardwareId
INNER JOIN Software
ON Software.SoftwareId = Titles_Software.SoftwareId
WHERE Titles.TitleId = {something}

You should be able to use a series of INNER JOINs to achieve your goal, like : 您应该能够使用一系列INNER JOIN来实现您的目标,例如:

select 
    t.titleName,
    h.hardwareName,
    s.softwareName
from titles t
inner join titles_hardware th on th.titleId = t.titleId
inner join hardware h on h.hardwareId = th.hardwareId
inner join titles_sotfware ts on ts.titleId = t.titleId
inner join software s on s.softwareId = ts.softwareId
where t.titleId = 'foo'

Maybe a UNION could work for you 也许UNION可以为您工作

DECLARE @TitleName VARCHAR(100) = 'The Boss Lady';

SELECT hard.HardwareName AS Name, 'hard' as TitleType, tit.TitleName
FROM Titles tit
JOIN Titles_Hardware tithard ON tithard.TitleId = tit.TitleId
JOIN Hardware hard ON hard.HardwareId = tithard.HardwareId
WHERE tit.TitleName = @TitleName

UNION ALL

SELECT soft.SoftwareName, 'soft', tit.TitleName
FROM Titles tit
JOIN Titles_Software titsoft ON titsoft.TitleId = tit.TitleId
JOIN Software soft ON soft.SoftwareId = titsoft.SoftwareId
WHERE tit.TitleName = @TitleName

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

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