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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:30:36+00:00 2026-06-03T21:30:36+00:00

Hello I need help in writing a query. Below is my DB structure id

  • 0

Hello I need help in writing a query.

Below is my DB structure

id  name    key    value
1   book1   key1    abc
2   book2   key2    aaa
3   book3   key5    abc
4   book4   key3    abc
5   book5   key2    aaa
6   book6   key2    aab
7   book7   key1    abc
8   book8   key2    abc
9   book9   key1    abc
10  book10  key1    abc

And I need those books whose key has combination key1 with value abc and key2 with value aaa.

SO it should return

1   book1   key1    abc
2   book2   key2    aaa
5   book5   key2    aaa
7   book7   key1    abc
9   book9   key1    abc
10  book10  key1    abc

book having key2 and value aab wont return.

P.s This is just an example structure I have made to demonstrate my need. Actually I am working on wordpress post meta table.

  • 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-03T21:30:39+00:00Added an answer on June 3, 2026 at 9:30 pm

    I think that according to your design at some point in time you’ll have several key + value pairs per book. This means that you might wish to select books where several specific keys has some particular values.

    This can be achieved with the following SQL (for the given example):

    SELECT t.id,t.name,k.key1,k.key2
      FROM test t
      JOIN (SELECT t.id,k1.value AS key1,k2.value AS key2
              FROM test t
              LEFT JOIN test k1 ON t.id=k1.id AND k1.`key`='key1'
              LEFT JOIN test k2 ON t.id=k2.id AND k2.`key`='key2'
      ) k ON t.id = k.id
    WHERE k.key1='abc' OR k.key2 = 'aaa';
    

    The inner part will create a pivoted table with keys in subject, while the outer part will perform the result filtering. You can play around with this setup here.

    Though, for such design, I would recommend splitting the table in subject into 2 separate ones: books and params. You can observe such setup here.
    Note, that if you’ll change filter condition to be k.key1='abc' AND k.key2 = 'aaa', then you’ll have only book10 matching.

    This example is quite simple, but it gives the idea.

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

Sidebar

Related Questions

Hello i am new in android and i really need help with something. I
Hello I need help with Regular Expression, I want to match each section (number
Hello I need some help with a bug in Internet Explorer 9. I am
Hello I need some help getting my mysql correct. I need to select one
Hello i need help on PHPMailer here is my code : $message = 'main
Hello freinds I need help as to how can I add 2 iFrames in
Hello everyone i need some help. I'm trying to make an application for mac
hello all i really need your help its my final project in university i
hello guys i need small help in understanding file system of android Now in
Hello stack overflow I need help with this problem. Ok, I have a flat

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.