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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:48:20+00:00 2026-05-23T06:48:20+00:00

Consider these two tables stored in a MySQL database. The first stores a list

  • 0

Consider these two tables stored in a MySQL database. The first stores a list of movies and the second stores metadata about each movie. Rather than adding ‘category’ and ‘actor’ fields to the movies table, they are stored as key/value pairs in the movie_attributes table, as each movie may have more than one category or actor attributed to it.

movies

id    name
-----------
0     hot rod
1     star wars

movie_attributes

id    movie_id    key      value
----------------------------------------
0     0           genre    comedy
1     0           genre    stupid
2     0           actor    andy samberg
3     0           actor    harrison ford
4     0           actor    chester tam
5     1           genre    adventure
6     1           actor    harrison ford

What would be the most efficient way to query the movies based on a search criteria that allows the user to select multiple genres and multiple actors? If the user adds more than one category to their search, I would like the results to include movies from both categories (but only those videos that also match the actors selected.)

For example, this pseudo-query:

select * from movies where (genre = 'comedy' OR genre = 'adventure') AND (actor = 'harrison ford')

would return this:

id    name
-----------
0     hot rod
1     star wars

Currently, my solution uses a mess of subqueries and it’s awfuly slow, as my database contains more than two attribute key types per movie (genres, actors, actresses, tags, etc..) and it’s terribly slow. Here’s the real query that’s equivalent to the example pseudo-query above:

select * from movies
    WHERE (select count(*) from movie_attributes 
           WHERE key='genre'
           AND (value='comedy' 
                OR value='adventure'))>0
    AND
          (select count(*) from movie_attributes 
           WHERE key='actor' 
           AND (value='harrison ford'))>0

Is there a more efficient way to achieve the same results using joins or some other SQL voodoo magic I’m unfamiliar with? 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-23T06:48:21+00:00Added an answer on May 23, 2026 at 6:48 am
    SELECT *
    FROM movies 
    WHERE EXISTS(
      SELECT NULL 
      FROM movie_attributes
      WHERE movies.id = movie_attributes.movie_id AND
            key = 'genre' AND value in ('comedy', 'adventure')
    ) AND 
    EXISTS(
      SELECT NULL 
      FROM movie_attributes
      WHERE movies.id = movie_attributes.movie_id AND
            key = 'actor' AND value = 'harrison ford'
    )
    

    I would however suggest against this kind of design, see @n8wrl answer.

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

Sidebar

Related Questions

Consider these two function definitions: void foo() { } void foo(void) { } Is
I have some doubts over how Enumerators work, and LINQ. Consider these two simple
Consider these two use cases. Say a store gives out store credit for customers.
Consider these classes. class Base { ... }; class Derived : public Base {
Consider these 3 table structures. Which will perform these queries the best. Structure 1
Consider these examples using print in Python: >>> for i in range(4): print('.') .
What is the correct way of iterating over a vector in C++? Consider these
(assume php5) consider <?php $foo = 'some words'; //case 1 print these are $foo;
How many records should there be before I consider indexing my sql tables?
I have a table in mysql which stores (among other columns) a bitmask as

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.