我试图在一个视图/表中将两个查询结果连接在一起。我试过这个代码很接近,但结果如下所示合并在一起。我需要它们是单独的列。我尝试过加入联盟和联盟,但似乎没有什么工作正常。
输出:
SQL查询:
`SELECT `PARTY DESCRIPTION`
FROM `PARTY`
WHERE
`PARTY CODE` IN (
SELECT `PARTY CODE`
FROM `PARTY CANDIDATE`
WHERE `CANDIDATE ID` IN (
SELECT `CANDIDATE ID`
FROM OFFICE
WHERE `OFFICE DESCRIPTION` = 'MAYOR'
)
)
UNION
(
SELECT `CANDIDATE NAME`
FROM `CANDIDATE`
WHERE `CANDIDATE ID` IN (
SELECT `CANDIDATE ID`
FROM OFFICE
WHERE `OFFICE DESCRIPTION` = 'MAYOR'
)
);`
行:5
PARTY DESCRIPTION
Republican
Democrat
Giuliani
Dinkins
Ferucci
我想要的是什么:
PARTY DESCRIPTION NAMES
Republican Giuliani
Democrat Ferucci
Dinkins
架构:
答案 0 :(得分:2)
如果我正确理解了这个问题,那么您正在尝试JOIN
两个查询,以便为持有市长办公室的候选人显示候选人名单和他们所属的各方。
您可以尝试这样的事情:
SELECT `PARTY DESCRIPTION`, `CANDIDATE NAME`
FROM `PARTY` p
JOIN `PARTY CANDIDATE` pc ON pc.`PARTY CODE` = p.`PARTY CODE`
JOIN `CANDIDATE` c ON pc.`CANDIDATE ID` = c.`CANDIDATE ID`
JOIN `OFFICE` o ON pc.`CANDIDATE ID` = o.`CANDIDATE ID`
WHERE `OFFICE DESCRIPTION` = 'MAYOR`