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 7660199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:26:01+00:00 2026-05-31T13:26:01+00:00

So, I want a SQL Query where I can enforce an object having multiple

  • 0

So,
I want a SQL Query where I can enforce an object having multiple associations
So i want to query all videos where they have all the tags i am limiting on

So Video must have a videos_tags that associates it to tags.id 1,2 and 3

so far I tried

SELECT videos.*, tags_videos.tag_id, tags_videos.video_id FROM videos
LEFT JOIN tags_videos ON tags_videos.video_id = videos.id
WHERE tags_videos.tag_id IN (1,2,3)

but this seems to be more of an OR clause not an AND clause is there and Exclusive and IN statement?

Also this is in Rails with active record. Is there a way to do this easily via AR in Rails?

Heres what i’ve tried

Video.all.select{|v| params[:tags].select{|t| v.tag_ids.include?(t.to_i)}.count == params[:tags].count }

  • 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-31T13:26:02+00:00Added an answer on May 31, 2026 at 1:26 pm

    If you want videos which have tags 1,2 and 3, envision what a result row would need to look like. You’ll realise you’ll need one column for each tag. To do this, you’ll need to join against tags_videos once for each tag you want to match.

    SELECT videos.* FROM videos
    INNER JOIN tags_videos t1 ON (t1.video_id = videos.id AND t1.tag_id=1)
    INNER JOIN tags_videos t2 ON (t2.video_id = videos.id AND t2.tag_id=2)
    INNER JOIN tags_videos t3 ON (t3.video_id = videos.id AND t3.tag_id=3)
    

    I’ve also used INNER JOIN rather then LEFT JOIN as you’re not interested in any that don’t match.

    Conceptually, you can think of this query as first getting all the videos which have tag 1, and then seeing which of those also have tag 2. So now we have videos with tag1+2, we can see which of those also have tag3. You can keep going for as many tags as you need.

    While I doubt it applies to your situation, one alternative method would to use a SET column type to store all the tags for a video, but this would only allow for 64 different tags. The advantage is that this column could be stored in the videos table, and thus a join would not be required (you’d just have a WHERE clause using FIND_IN_SET multiple times)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want my SQL query can run like this. I have two tables: Key1,
I have the following sql query and I want to filter the results where
I want to escape all the special character is an sql query. So i
i have table id pagenane username i want this sql query select pagename, count(*)
In HQL, how can I use bitwise operators? I want the resulting SQL query
I want to run a raw SQL query as following: ActiveRecord::Base.connection.execute(some_query); Can I capture
I don't want to use not in this sql query. How can I do
How can I improve the SQL query below (SQL Server 2008)? I want to
I have a sql query shown below i want to use the variables streetname,
I can't get this sql query right... I want the top 5 latest comments

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.