繁体   English   中英

帮助结合这两个查询

[英]Help combining these two queries

我需要一个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')))

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最简单。

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'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM