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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:10:51+00:00 2026-06-09T15:10:51+00:00

I have the following tables: Widgets Fields Widgets_Fields Widgets table is a simple product/listing

  • 0

I have the following tables:

  • Widgets
  • Fields
  • Widgets_Fields

Widgets table is a simple product/listing table with an id, user_id, added/updated, etc.

Fields table lists field names for widgets. There can be any number of fields (e.g., height, width, price, year, colour, etc.). Fields table:

  • ID
  • name

Each widget can have any number of fields and values associated with it. The Widgets_Fields table will look like so:

  • ID
  • field_id
  • widget_id
  • value

If you simply want to get all of the fields for a widget or group of widgets, it is pretty straight forward:

select widget_id from widgets_fields where widget_id = xxx

But what if you are searching for widgets and want to find only those widgets that have certain values for their fields? Also doable

select widget_id from widgets_fields where field_id = xxx and value = xxx

My problem arises when I want to select widgets based upon multiple fields. For example, all widgets that are either ‘red’ or ‘orange’ AND also have a year of 2012 or 2011

select widget_id from widgets_fields where 
(field_id = xxx and (value = 'red' or value = 'orange')
and
(field_id = xxy and (value = 2012 or value = 2011)

This produces an impossible where. Alternatively I could do something like this

select widget_id from widgets_fields where
field_id in (xxx, xxy)
and value in ('red', 'orange', 2012, 2011)

but it won’t necessarily return what I want as it will be looking for the given values in any of the provided field ids.

So, finally, my question is whether or not there is any way to properly build a query to accomplish this? I’m certain there must be, but I’ve been working on it for a while and have yet to come up with a solution.

  • 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-09T15:10:52+00:00Added an answer on June 9, 2026 at 3:10 pm
    all widgets that are either 'red' or 'orange' 
    AND also have a year of 2012 or 2011
    

    can be accomplished via subquerying and INNER JOINing the results together

    SELECT widget_id FROM
    (SELECT widget_id FROM widgets_fields
    WHERE field_id = 'color' and value in ('red', 'orange')) ResultSetA
    INNER JOIN
    (SELECT widget_id FROM widgets_fields
    WHERE field_id = 'year' and value in (2011, 2012)) ResultSetB
    ON ResultSetA.widget_id = ResultSetB.widget_id
    

    in plain words, this would be “get the id of all red and orange widgets, then get the id of all widgets from 2011 or 2012, then give me the ids which appear in both lists.

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

Sidebar

Related Questions

In PostgreSQL 8.3, let's say I have a table called widgets with the following:
I have following tables questions -> id, question_data, user_id users -> id, fname, lname
Suppose that we have following tables create table Employee( 2 EMPNO NUMBER(3), 3 ENAME
I have following tables: Profiles: ProfileID (Primary Key) ProfileName, etc. Contacts: ContactID (Primary Key)
Say I have the following table: TABLE: product =============================================================================== | product_id | language_id |
I have a fairly simple table called widgets. Each row holds an id, a
I have the following tables: users +----------+----------+----------+ | id | name | dob |
I have the following tables in my database: Products ID_PRODUCT PRODUCTNAME PRICE Customers ID_CUSTOMER
I have the following tables. I want to run a query but I think
I have the following tables. I wanted it so when I make more boxes

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.