SQL艺术家类似

时间:2014-04-29 10:12:50

标签: sql

我有5张桌子:

 Artists (artistID: int, name: varchar(255))
 SimilarArtists (artistID: int, simArtistID: int, weight: int)
 Albums (albumID: int, artistID: int, name: varchar(255))
 Tracks (trackID: int, artistID: int, name: varchar(255), length: int)
 TrackLists (albumID: int, trackID: int, trackNum: int)

找出所有与Mogwai更相似但与Nirvana相似的艺术家的名字(意思是他们与Mogwai相似的重量更大)

SELECT Artists.name
FROM Artists, SimiliarArtists
WHERE Artists.ArtistID = SimiliarArtists.ArtistID AND
SimiliarArtists.weight > (
SELECT SimiliarArtists.weight
FROM Artists, SimiliarArtists
WHERE Artists.name = ‘Mogwai’ AND
Artists.ArtistID = SimiliarArtists.ArtistID);

2 个答案:

答案 0 :(得分:0)

暴力查询:),请检查空值

select * from (  
  select 
    (select weight from similiarartists sa, artists aa where sa.simartistid = aa.artistid and sa.artistid = a.artistid and aa.name = 'Nirvana') nirvana,
    (select weight from similiarartists sa, artists aa where sa.simartistid = aa.artistid and sa.artistid = a.artistid and aa.name = 'Mogwai') mogwai,
    a.*
 from artists a
) where a.mogwai > a.nirvana

答案 1 :(得分:0)

这是我对此的尝试;

select *
from artists a
left join 
(select sa.artistid, weight from similiarartists sa, artists aa where sa.simartistid =  aa.artistid and sa.artistid = a.artistid and aa.name = 'Nirvana') nirvana on nirvana.artistid = a.artistid
left join 
(select sa.artistid, weight from similiarartists sa, artists aa where sa.simartistid = aa.artistid and sa.artistid = a.artistid and aa.name = 'Mogwai') mogwai on mogwai.artistid = a.artistid
where mogwai.weight > nirvana.weight;