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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:29:26+00:00 2026-05-12T18:29:26+00:00

Alright, let’s say I have these two tables: items with columns id, stuff item_properties

  • 0

Alright, let’s say I have these two tables:

  • items with columns id, stuff
  • item_properties with columns item_id, prop_id

Now I want to execute a query like

SELECT stuff FROM items WHERE
      EXISTS(SELECT * FROM item_properties WHERE prop_id = 123 AND item_id = items.id)
  AND EXISTS(SELECT * FROM item_properties WHERE prop_id = 456 AND item_id = items.id)
  AND NOT EXISTS(SELECT * FROM item_properties WHERE prop_id = 789 AND item_id = items.id)
  AND NOT EXISTS(SELECT * FROM item_properties WHERE prop_id = 101 AND item_id = items.id)

Which works, but looks ugly and is slow. Can anyone think of a smarter way to do this? I can also get the 123,456 and 789,101 lists via a subquery from a third table, if necessary. I am open to suggestions to change my table design as well.

The number of property IDs I need to check the properties of an item against can vary.

Thanks!

  • 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-12T18:29:27+00:00Added an answer on May 12, 2026 at 6:29 pm

    if you have a table that contains prop_id to include and to exclude itemsIU (item_id, prop_id, include)

    select distinct stuff 
      from items i
      join item_properties ip on i.id = ip.item_id
      join itemsIU iiu on ip.prop_id = iiu.prop_id
    group by i.id
    having sum(include) = (select count(1) 
                             from itemsIU iiu2 
                            where i.id = iiu2.item_id
                              and iiu2.include = 1)
    

    for you particular example you can use:

    select distinct stuff 
      from items i
      join item_properties ip on i.id = ip.item_id
      join (          select 123 prop_id, 1 include
            union all select 456, 1
            union all select 789, 0
            union all select 101, 0) iiu on ip.prop_id = iiu.prop_id
    group by i.id
    having sum(include) = 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright. So I have a very large amount of binary data (let's say, 10GB)
Alright let me explain my situation first: I am part of an organization that
Alright, currently I have my SWF hitting a php file that will go and
Alright, I have been doing the following (variable names have been changed): FileInputStream fis
Alright, I know how the fieldset / legend works out in HTML. Say you
Alright. I have a query that looks like this: SELECT SUM(`order_items`.`quantity`) as `count`, `menu_items`.`name`
Alright. So I figure it's about time I get into unit testing, since everyone's
Alright, I'm trying to read a comma delimited file and then put that into
Alright, after doing a ton of research and trying almost every managed CPP Redist
Alright, quick question. A server is running in Eastern Time. PHP program needs to

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.