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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:10:40+00:00 2026-06-16T00:10:40+00:00

I have a cross-reference table: ID | tag 1 | 4 1 | 5

  • 0

I have a cross-reference table:

ID | tag
1  | 4
1  | 5
1  | 6
2  | 4
2  | 5
2  | 8
3  | 2

I need to select the IDs that match all of a set of tags. For example, if I were given the tags '4','5' I would get IDs '1','2'. If I were given the tags '4','2' I would not get any IDs because there were no IDs that matched all of the tags.

Also, if I were given the tags '4','9' then I also should not get any resulting IDs because a search for '9' would result in a NULL value, and therefore no IDs match all the tags.

I’ve been pulling my hair out for the last 2 days. Hopefully someone can help me.

  • 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-16T00:10:41+00:00Added an answer on June 16, 2026 at 12:10 am

    The idea of the query is that you need to match the number of records to the number of values you have provided in the WHERE clause.

    SELECT ID
    FROM tableName
    WHERE tag IN (4, 8)
    GROUP BY ID
    HAVING COUNT(*) = 2
    
    • SQLFiddle Demo

    if unique constraint was not specified on tag for every ID, then DISTINCT is needed

    SELECT ID
    FROM tableName
    WHERE tag IN (4, 8)
    GROUP BY ID
    HAVING COUNT(DISTINCT tag) = 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a cross reference table that looks like this: id document_id subject_id 1
I have a cross reference table and need to find common values of one
I have a cross reference table RolePrivilege that has FK to Role and Privilege
I have 4 tables (appointed, class, elected, status) that I want to cross reference
I have a table with composite primary keys. This is a cross reference table
I have a database with three tables and I need to cross reference the
I have a pivot table with some data that im trying to reference with
I have a cross compiler that specifies a custom loader in its .interp header:
I have a website that is mirrored cross two sub-domains. So I have separate
I have a web page that uses cross page posting to post to a

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.