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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:51:48+00:00 2026-05-31T03:51:48+00:00

I am trying to construct a MySQL query that will allow me to pull

  • 0

I am trying to construct a MySQL query that will allow me to pull the song title of a track in the database that has a genre of both Pop and Electronic.

+----------------------------------------------------+
TABLE: SONG
+----------------------------------------------------+
song_id  | title       |  
1        | song name   | 


+----------------------------------------------------+
TABLE: GENRE
+----------------------------------------------------+
genre_id   | name       | 
1          | Pop        |    
2          | Electronic |   


+----------------------------------------------------+
TABLE: SONG_GENRE
+----------------------------------------------------+
genre_id  | song_id | 
1         | 1       | 
2         | 1       | 

This SQL doesn’t work, as it is obviously never going to return both a genre_id of 1 and 2 but this is where I am stuck.

SELECT DISTINCT song.song_id, song.title
                    FROM song
                    LEFT JOIN song_genre ON song_genre.song_id = song.song_id
                    LEFT JOIN genre ON genre.genre_id = song_genre.genre_id
                    WHERE genre.genre_id ='1'
                    AND genre.genre_id='2'

If somebody could point me in the right direction I would be most greatful!

  • 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-31T03:51:49+00:00Added an answer on May 31, 2026 at 3:51 am

    Here’s one way to do it:

        SELECT DISTINCT song.song_id, song.title
        FROM song
        INNER JOIN (SELECT songid FROM song_genre WHERE song_genre.genre_id ='1') genre1 
             ON genre1.song_id = song.song_id
        INNER JOIN (SELECT songid FROM song_genre WHERE song_genre.genre_id ='2') genre2 
             ON genre2.song_id = song.song_id
    

    Another way that might be more efficient. This assumes there is no dups in song_genre. COUNT(*) = X where X equals the number of genres listed.

    SELECT DISTINCT song.song_id, song.title
    FROM song
    INNER JOIN (SELECT songid, COUNT(*) FROM song_genre 
    WHERE genre_id IN ('1','2') 
    GROUP BY songid HAVING COUNT(*) = 2) genre1 ON genre1.song_id = song.song_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to construct a MySQL query that will only return unassigned tasks. A
I'm trying to construct a query that will include a column indicating whether or
I am trying to construct a mysql query string to pull out certain records
I'm using MySQL and I'm trying to construct a query to do the following:
I find that when trying to construct complex MySQL joins and groups between many
I am trying to construct the SQL query, that should be executed as is
Am trying to construct a simple update query in my model class Model_DbTable_Account extends
I am trying to construct a Groovy statement to find values that don't exist
I'm trying to create a simple class based on my database PDO class that
I am trying to create a class with various methods that will need to

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.