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

  • Home
  • SEARCH
  • 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 1013471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:06:24+00:00 2026-05-16T10:06:24+00:00

I have a listings database. I also have a listings_attributes database that looks like

  • 0

I have a listings database. I also have a listings_attributes database that looks like this:

id | listing_id | attribute_id

Basically the listings_attributes table stores the attributes that are assigned to a certain listing.

I am wanting to filter listings depending on what attributes they have.

An example would be I want to find all the listings with an attribute_id of 1. That’s easy.

SELECT l.id FROM listings AS l 
    LEFT JOIN listings_attributes AS la ON l.id = la.listing_id 
    WHERE la.attribute_id = 1
    GROUP BY l.id

What my problem is how do I pull out listings that have a an attribute_id of 1 and 2. If there are listings with an attribute_id of 1 but doesn’t have an attribute_id of 2, I don’t want these to be pulled out of the database.

However, if there are listings with an attribute_id of 1, 2 and 3 I would like these to be pulled out of the database.

Here is my SQL that I was playing with but the “OR” between attribute_id’s doesn’t work and neither does “AND”.

SELECT l.id FROM listings AS l 
    LEFT JOIN listings_attributes AS la ON l.id = la.listing_id 
    WHERE (la.attribute_id = 1 OR la.attribute_id = 2) 
    GROUP BY l.id

What code would I need to make it work the way I’ve explained above?

  • 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-16T10:06:25+00:00Added an answer on May 16, 2026 at 10:06 am

    When looking for multiple attributes, you need to use a GROUP BY with an OR to make sure that the attributes both exist in the listings_attributes table, like you already have. Then you add a HAVING clause to make sure that more than one row matches. Like so:

    SELECT l.id FROM listings AS l  
      LEFT JOIN listings_attributes AS la ON l.id = la.listing_id 
      WHERE (la.attribute_id = 1 OR la.attribute_id = 2) 
      GROUP BY l.id HAVING COUNT(*) = 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a site that lists business listings from a database. On each page
I have a database with ~100,000 entries - right now my query looks like
I am trying to extract duplicate rows from my database. I have a listings
Let's say that I have a vast quantity of restaurant reviews in a database
I have a script that appends some rows to a table. One of the
I have a table Classes, which stores data on different college classes (a unique
Ok, I have a database with with a table for storing classified posts, each
I have a database of exhibition listings related by foreign key to a database
I have googled and fiddled with this for hours! I have a map that
I have a database filled with records and by using this MySQL-syntax: public static

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.