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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:52:55+00:00 2026-06-10T09:52:55+00:00

I have a table called Properties (pid, uid, pname, pvalue). The pid column is

  • 0

I have a table called Properties (pid, uid, pname, pvalue). The pid column is auto generated. Each uid (user id) could have multiple name value pairs stored in the pname and pvalue.

As an input I’ve multiple name value pairs for pname and pvalue which forms a complicated boolean expression.

For example: let’s start w/ one name value pair. Say I want to retrieve all uid’s whose ‘favorite_color’ is ‘red’.

I wrote an SQL query:

SELECT * 
  FROM properties 
 WHERE ((pname = 'favorite_color') and (pvalue = 'red'))

The query soon gets complicated if I had to retrieve something like, fetch all uid’s whose ‘favorite_color’ is ‘red’ or ‘blue’ and ‘favorite_drink’ is ‘juice’ or ‘ ‘milk’ and ‘favorite_ hobby’ is ‘music’ or ‘art’ etc.

I wrote an SQL query:

SELECT * 
  FROM properties 
 WHERE (((pname = 'favorite_color') and (pvalue = 'red')) 
    OR ((pname = 'favorite_color') and (pvalue = 'blue'))) 
   AND (((pname = 'favorite_drink') and (pvalue = 'juice')) 
    OR ((pname = 'favorite_drink') and (pvalue = 'milk'))) 
   AND (((pname = 'favorite_hobby') and (pvalue = 'music')) 
    OR ((pname = 'favorite_hobby') and (pvalue = 'art')))

I got the expression correct but unfortunately it fails because the evaluation is done on each row. What if I wanted to add more name value pairs to the where clause?

Questions:

  1. Is it possible to write and SQL query for this?

  2. The other idea I had was to fetch all the pname, pvalue pairs for each user, build a dynamic expression using an expression language and my input name value paris to evaluate it. I’ve apache’s JEXL in mind.

  • 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-10T09:52:57+00:00Added an answer on June 10, 2026 at 9:52 am

    To do the ANDs you need to do as many self-joins as you have and-ed conditions:

    SELECT *
    FROM Properties p1, Properties p2, Properties p3
    WHERE p1.uid = p2.uid AND p1.uid = p3.uid
    AND (p1.pname = 'favorite_color' AND p1.pvalue IN ('red', 'blue'))
    AND (p2.pname = 'favorite_drink' AND p2.pvalue IN ('juice', 'milk'))
    AND (p3.pname = 'favorite_hobby' AND p2.pvalue IN ('music', 'art'))
    

    EDIT:

    Another possibility is to denormalize the data, and then use FIND_IN_SET() or RLIKE:

    SELECT uid, group_concat(concat(pname, '=', pvalue)) props
    FROM Properties
    GROUP BY uid
    HAVING props RLIKE 'favorite_color=(red|blue)'
    AND props RLIKE 'favorite_drink=(juice|milk)'
    AND props RLIKE 'favorite_hobby=(music|art)'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table called Buttons. Buttons table i have column button_number . Table contain
I have table called posts in my DB. Each post has field called social_network
I have a table with several million rows. Each row represents a user session
I have a table called People with a columns of datatype xml called properties
I have a table of entities called MyItems, and each item in this table
I have a table called Users in my database. Let's assume that User has
I have table called stats . In am inserting yes or no in the
i have table called as Support, which have a field named Name and contains
I have a table called 'MyTable' that looks like: ID | Item | Type
I have a table called States and i am displaying values of states in

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.