2

我想对 Oracle DB 中的两个表进行减号操作。第一个表是 Chart_Year,其中包含 trackPosition、trackArtist、trackName、previewLink。另一个表是 Recommendation,其中只有 trackArtist、trackName、previewLink。我希望按 trackPosition 订购决赛桌。我这样做的方式是通过 trackArtist 订购:

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...

?

4

1 回答 1

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
;
于 2012-09-23T14:55:36.247 回答