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

  • Home
  • SEARCH
  • 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 1010887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:09:07+00:00 2026-05-16T09:09:07+00:00

Yeah, kinda dumb title, but I found it hard to describe my problem. I

  • 0

Yeah, kinda dumb title, but I found it hard to describe my problem. I have the following tables:

properties

id | name
1  | color
2  | material

options

id | property_id | name
1  | 1           | yellow
2  | 1           | blue
3  | 2           | wood
4  | 2           | stone

substances

id | name
1  | orange juice
2  | cheese

relations

id | substance_id | option_id
1  | 2            | 1
2  | 2            | 3
3  | 1            | 1

Now, I have a list of options and want to know which substances are related to all those options. (For example, which substances are yellow and made of wood?) Is this possible with one query?

I’m trying to do this in Rails.

  • 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-16T09:09:07+00:00Added an answer on May 16, 2026 at 9:09 am
    (SELECT s.name FROM substances s, relations r, options o
    WHERE r.substance_id = s.id and r.option_id = o.id and o.name='yellow')
    INTERSECT
    (SELECT s.name FROM substances s, relations r, options o
    WHERE r.substance_id = s.id and r.option_id = o.id and o.name='wood')
    

    Or

    SELECT s.name FROM substances s
    WHERE exists(SELECT * from relations r, options o 
          WHERE r.substance_id = s.id and r.option_id = o.id and o.name='yellow')
    AND   exists(SELECT * from relations r, options o 
          WHERE r.substance_id = s.id and r.option_id = o.id and o.name='wood')
    

    Having a single options table with the property_id to tell apart different kinds of options is not such a good idea and really makes this much harder. I’d suggest breaking out the different kinds of options into different tables

    MATERIALS(id,name)
    COLORS(id,name)
    

    and using separate relations for each type of table. In this case, you wouldn’t need separate tables for each relation, since it appears that it’s a many (substance) to one (color) relation.

    SUBSTANCES(id, name, material_id, color_id)
    

    Then your query is much simpler

    SELECT s.name FROM substances s, materials m, colors c
    WHERE s.color_id = c.id AND m.material_id = m.id
    AND m.name = 'wood'
    AND c.name = 'yellow'
    

    ActiveRecord should easily be able to handle this last query much more easily than the first two.

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

Sidebar

Related Questions

Yeah, I did my homework and I found the setFrame:frame display:YES animate:YES but I
Yeah, the noob question of the century... But in all seriousness, I actually have
I can't describe the problem well enough in the title. If somone has edit
Yeah this works in FF and Chrome, but for some reason wont work in
Yeah, looks insane, but let me explain better. When in Jquery we use for
I have a very large list Suppose I do that (yeah, I know the
So I have Visual Studio 2003 running on Windows 7 - yeah I am
Yeah, kind of random, but I was wondering if anyone could link me to
Yeah. As the title says, I am tasked with creating custom field in user
Hello yeah I'm asking this question a second time, sorry about that but I

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.