Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 769333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:19:07+00:00 2026-05-14T18:19:07+00:00

I need a SQL query that returns results matched by EITHER of the following

  • 0

I need a SQL query that returns results matched by EITHER of the following SQL queries:

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

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

Here’s what I tried, but it doesn’t work:

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:19:08+00:00Added an answer on May 14, 2026 at 6:19 pm

    UNION is the simplest. If your engine’s optimizer doesn’t do an efficient job with that, I might look deeper (but anything else would probably involve a lot of LEFT JOINs, since it looks you are joining two distinct uses of annotations):

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

    For the same reason your INNER JOIN attempt failed (all join criteria must be satisfied), you would have to change almost everything to LEFT JOIN (or LEFT JOIN to a nested INNER JOIN) – I think the UNION is simplest.

    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'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a Java function that returns the results of a SQL SELECT query
I have the following Sql Query that returns the type of results that I
Basically, the question says it all. I need a PL\SQL query that returns a
I currently have a SQL query that returns a number of fields. I need
I need to be able to have an SQL query that searches my database
Using Microsoft SQL server 2008 I have a query that is in need of
I work on AI chat bot. I need sql query to select a single
I need an sql query to give me the following: All the values in
I need some help with SQL Query. I am trying to select all records
My SQL query returns results with 4 columns A, B, C, D. Suppose the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.