帮助组合这两个查询

时间:2010-05-03 01:59:37

标签: sql mysql ruby-on-rails

我需要一个SQL查询,它返回以下SQL查询的EITHER匹配的结果:

查询1:

SELECT "annotations".* FROM "annotations" INNER JOIN "votes" ON "votes".voteable_id = "annotations".id AND "votes".voteable_type = 'Annotation'
WHERE (votes.vote = 't' AND votes.voter_id = 78)

查询2:

SELECT "annotations".* FROM "annotations" INNER JOIN "songs" ON "songs".id = "annotations".song_id INNER JOIN "songs" songs_annotations ON "songs_annotations".id = "annotations".song_id INNER JOIN "users" ON "users".id = "songs_annotations".state_last_updated_by_id
WHERE (annotations.referent IS NOT NULL AND annotations.updated_at < '2010-04-05 01:51:24' AND (body = '?' OR body LIKE '%[?]%') AND ((users.id = songs.state_last_updated_by_id and users.needs_edit = 'f' and songs.state != 'work_in_progress') OR (songs.state = 'published'))

这是我尝试过的,但它不起作用:

SELECT "annotations".* FROM "annotations" INNER JOIN "songs" ON "songs".id = "annotations".song_id INNER JOIN "songs" songs_annotations ON  "songs_annotations".id = "annotations".song_id INNER JOIN "users" ON "users".id = "songs_annotations".state_last_updated_by_id  INNER JOIN "votes" ON "votes".voteable_id = "annotations".id AND "votes".voteable_type = 'Annotation' WHERE ((votes.vote = 't' and votes.voter_id = 78) OR (annotations.referent IS NOT NULL and annotations.updated_at < '2010-04-05 01:43:52' and (annotations.body = '?' OR annotations.body LIKE '%[?]%') and ((users.id = songs.state_last_updated_by_id and users.needs_edit = 'f') OR songs.state = 'published')))

1 个答案:

答案 0 :(得分:1)

UNION是最简单的。如果你的引擎的优化器没有做那么高效的工作,我可能看起来更深(但其他任何东西都可能涉及很多LEFT JOIN,因为它看起来你加入了两个不同的注释用途):

SELECT "annotations".*
FROM "annotations"
INNER JOIN "votes"
    ON "votes".voteable_id = "annotations".id 
    AND "votes".voteable_type = 'Annotation'
WHERE (votes.vote = 't' AND votes.voter_id = 78)
UNION
SELECT "annotations".*
FROM "annotations"
INNER JOIN "songs"
     ON "songs".id = "annotations".song_id
INNER JOIN "songs" songs_annotations
     ON "songs_annotations".id = "annotations".song_id
INNER JOIN "users"
     ON "users".id = "songs_annotations".state_last_updated_by_id
WHERE (annotations.referent IS NOT NULL 
     AND annotations.updated_at < '2010-04-05 01:51:24' 
     AND (body = '?' OR body LIKE '%[?]%')
     AND ((users.id = songs.state_last_updated_by_id and users.needs_edit = 'f' and songs.state != 'work_in_progress') OR (songs.state = 'published'))

出于同样的原因,您的INNER JOIN尝试失败(必须满足所有连接条件),您必须将几乎所有内容更改为LEFT JOIN(或LEFT JOIN到嵌套的INNER JOIN) - 我认为UNION最简单。 / p>

SELECT "annotations".*
FROM "annotations"
LEFT JOIN "votes"
    ON "votes".voteable_id = "annotations".id 
    AND "votes".voteable_type = 'Annotation'
LEFT JOIN "songs"
     ON "songs".id = "annotations".song_id
LEFT JOIN "songs" songs_annotations
     ON "songs_annotations".id = "annotations".song_id
LEFT JOIN "users"
     ON "users".id = "songs_annotations".state_last_updated_by_id
WHERE (votes.vote = 't' AND votes.voter_id = 78)
     OR
     (annotations.referent IS NOT NULL 
     AND annotations.updated_at < '2010-04-05 01:51:24' 
     AND (body = '?' OR body LIKE '%[?]%')
     AND ((users.id = songs.state_last_updated_by_id and users.needs_edit = 'f' and songs.state != 'work_in_progress') OR (songs.state = 'published'))