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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:51:30+00:00 2026-06-06T06:51:30+00:00

I have an objects, tags, and tag_relationships table. Objects and tags each have an

  • 0

I have an objects, tags, and tag_relationships table. Objects and tags each have an id column, and tag_relationships connects the two.

Sample of tag_relationships (used in example below):

+--------+--------+
| obj_id | tag_id |
+--------+--------+
| 1      | 1      |
| 1      | 2      |
| 2      | 1      |
| 2      | 3      |
| 2      | 4      |
+--------+--------+

I want to implement an AND search to allow people to filter tags. Only obj_ids that have a row with the all of the given tag_ids should be returned. For example:

Sample input: 1, 3 (would actually be tag names, but they would converted to ids)

Desired output: 2

It feels like this should be easy, but for the life of me I can’t figure out a simple way to do this. I’m currently thinking of returning a table with all the rows in tag_relationships that have one of the given tag_ids, then sorting through them with PHP. Is there a simpler, SQL-only method?

  • 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-06-06T06:51:31+00:00Added an answer on June 6, 2026 at 6:51 am

    You can use the HAVING clause in conjunction with GROUP BY to answer questions involving all of something…

    SELECT
        obj_id
    FROM
        tag_relationships
    WHERE
        tag_id IN (1,3)
    GROUP BY
        obj_id
    HAVING
        COUNT(*) = 2
    

    The 2 in the COUNT(*) represents the number of tags you are searching on. If you wanted objects that satisfied say, three tags… (e.g. 1,3,6), you would up the COUNT(*) to 3.

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

Sidebar

Related Questions

I have an array of Card objects (NSObjects), each with a field called tags
I have two persistence objects in my app: Things and tags attached to things.
Say I have objects within the Canvas with a tags 'stopped' and 'flying' .
I want to check objects in a List for their tags. And for each
I've got two tables (articles and tags) that have a one-to-many relationship. I remember
I have two listviews, one for Images and one for Tags, which have a
So in my domain I have 3 objects. User, Item and Tag. Each user
Let's say I have a database full of Tag objects. Each Tag has an
I have Hibernate Objects defined as Class SomeText{ private Long textId; private Set<Tag> Tags
If I have a set of tags (<100), and a set of objects (~25000),

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.