我想对Oracle DB中的两个表执行减号操作。第一个表是Chart_Year,其中包含trackPosition,trackArtist,trackName,previewLink。另一个表是推荐,它只跟踪艺术家,trackName,previewLink。我希望通过trackPosition对最终表进行排序。我的方式是按轨道艺术家的顺序:
SELECT trackartist, trackname, previewlink
FROM
(
SELECT
trackposition, trackartist, trackname, previewlink
FROM
chart_year
WHERE
chartsYear between 2004 and 2008
ORDER BY trackposition ASC
)
MINUS
SELECT
trackartist, trackname, previewlink
FROM
recommendation
WHERE
user = 'bla'
也许我可以做类似
的事情SELECT
1 as trackPosition, trackartist, trackname, previewlink
FROM
recommendation...
答案 0 :(得分:1)
您可以使用MINUS
:
EXISTS
SELECT trackartist, trackname, previewlink
FROM chart_year cy
WHERE chartsYear BETWEEN 2004 AND 2008
AND NOT EXISTS
( SELECT 1
FROM recommendation r
WHERE r.user = 'bla'
AND r.trackartist = cy.trackartist
AND r.trackname = cy.trackname
AND r.previewlink = cy.previewlink
)
ORDER
BY trackposition ASC
;
或LEFT OUTER JOIN
:
SELECT cy.trackartist, cy.trackname, cy.previewlink
FROM chart_year cy
LEFT
OUTER
JOIN recommendation r
ON r.user = 'bla'
AND r.trackartist = cy.trackartist
AND r.trackname = cy.trackname
AND r.previewlink = cy.previewlink
WHERE cy.chartsYear BETWEEN 2004 AND 2008
AND r.user IS NULL -- i.e., where the join failed
ORDER
BY cy.trackposition ASC
;