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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:41:51+00:00 2026-06-18T22:41:51+00:00

I have a table in the following format: row_key extID tag val ——- —–

  • 0

I have a table in the following format:

row_key  extID  tag   val
-------  -----  ---   ---
1         1     A     a
2         1     A     b
3         1     B     c
4         2     A     d
5         2     C     e

Now I want to have all extID’s where there are several pairs of (tag, val) with specific values, for example:

(tag, val) = (A,a) AND (tag, val) = (B,c)

or,

(tag, val) = (C,e)

The number of constrains can change.

I can think of several ways to do this:

  1. Perform a self-join for each constraint
  2. Do the searching (iteratively) in the caller program (multiple SQL queries)
  3. (Maybe?) write a SQL function to do this
  4. Nested SELECT clauses (passing to the outer level the “extID” and using WHERE extID IN (SELECT extID FROM ...)
  5. The only true solution that I just can’t find.

Which one would be the preferred (fastest and most elegant) way to do this? (Except, of course, “Surely, 5. is the correct answer.”)

I think a multiple SELF-join is quite elegant. However, I do not know if it is fast and comparatively memory-efficient.

Further, I would like to use a way that works with MySQL, PostgreSQL and SQLite without adaptation – That’s why I can’t use PIVOT afaiu.

  • 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-18T22:41:52+00:00Added an answer on June 18, 2026 at 10:41 pm
    SELECT  extID
    FROM    tableName
    WHERE   (tag = 'A' AND val = 'a') OR
            (tag = 'B' AND val = 'c')
    GROUP   BY extID
    HAVING  COUNT(*) = 2
    
    • SQLFiddle Demo
    • SQL of Relational Division

    UPDATE 1

    since you haven’t mentioned that there can be duplicate combination of tag and val, DISTINCT keyword is needed.

    SELECT  extID
    FROM    tableName
    WHERE   (tag = 'A' AND val = 'a') OR
            (tag = 'B' AND val = 'c')
    GROUP   BY extID
    HAVING COUNT(DISTINCT tag, val) = 2
    
    • SQLFiddle Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php query the returns the following JSON format from a table.
I have a table (items) which is in the following format: ITEMNO | WEEKNO
I have a mysql database with a table in the following format: ID: 1
I have a few rows in my table in the following format, <tr class
I have the following table in csv format in Excel: 1 1 1 2
I currently have a table in the following format and with the following example
I have the following table format and would like to be able to add
I have a page that contains a table with the following format: <table id=gigsTable>
I have a table with the following columns: match_id, player_slot, ability, level There are
I am fairly new to Indexes. I have table following table [FORUM1] [msg_id] [int]

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.