SQL Query将父表与多个子行连接成一行

时间:2018-01-31 11:21:22

标签: sql-server

我正在尝试实现查询以从数据库中获取设置信息,其中一个设置链接到多个项目类型:控制器,平台和KVM设备。

这样:

         Rack 
           |
          Setup 
  /        |         \
controller Platform   KVM

父级和子级之间的连接存储在另一个表中。

我尝试了bellow查询,但是我在相隔的行中设置了子设置。 如何改进查询以解决此问题?

查询:

 select R.Id as Rack_Id, 
 S.Id,C.Id as Setup_Id, 
 P.Id as Platform_Id, 
 K.Id as KVM_Id, 
 R.Capacity, R.[Rack location],
 S.[Location in Rack], C.[Controller IP], P.ISMP, P.Platform
 From #Racks R join LinksAre LR on LR.SourceId = R.Id
 join #Setups S on LR.TargetId=S.Id
 left join LinksAre LS on LS.SourceId = S.Id
 left join #KVMs K on LS.TargetID = K.Id
 left join #Controllers C on LS.TargetId=C.Id
 left join #Platforms P on LS.TargetId = P.Id
 order by R.Id, S.Id

谢谢!

1 个答案:

答案 0 :(得分:0)

认为您可能需要子查询:

# selenium imports
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import random

PROXY ="88.157.149.250:8080";


chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
# //a[starts-with(@href, 'https://www.amazon.com/')]/@href
LINKS_XPATH = '//*[contains(@id,"result")]/div/div[3]/div[1]/a'
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.get(
    'https://www.amazon.com/s/ref=lp_11444071011_nr_p_8_1/132-3636705-4291947?rh=n%3A3375251%2Cn%3A%213375301%2Cn%3A10971181011%2Cn%3A11444071011%2Cp_8%3A2229059011')
links = browser.find_elements_by_xpath(LINKS_XPATH)
for link in links:
    href = link.get_attribute('href')
    print(href)