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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:21:06+00:00 2026-05-25T15:21:06+00:00

Here’s my problem: I have three tables that represent a many to many relationship

  • 0

Here’s my problem: I have three tables that represent a many to many relationship which look like to following:

table products
product_id
product_price, etc. (basic info same for every product)

table specifications
spec_id
spec_name

table products_specifications
product_id
spec_id
content

My goal is to select products by their specifications (one as well as multiple) so I used the following query:

SELECT *
FROM products p
JOIN products_specifications ps ON ps.products_id = p.products_id
JOIN specifications s ON s.spec_id = ps.spec_id
WHERE s.spec_name IN (<names of specs>)
AND pts.content IN (<content of specs>)
GROUP BY p.products_id
HAVING COUNT(DISTINCT s.specifications_name) = <count of specs name>
AND COUNT(DISTINCT ps.content) = <count of specs content>

I would like to get ALL of the specifications that belong to ALL products, however this query only returns one of the specifications (one that is used to make the query).

The specifications.content column contains the values that I want to use to query a product (or multiple) with (included with all it’s specifications). The name of these specifications can be found specifications.spec_name table. To make sure the query does not retrieve content that is possibly the same in another row but from a different specification I also do a WHERE IN of the names of the specifications.

Is it possible make a query that selects on certain specifications, but also returns ALL specifications of the selected product?

I have thought of and experimented with UNIONS and SUBQUERIES. However I wasn’t able to produce a query that sufficed. I think a subquery is probably the direction to go in. A second query with the returned product_id is not a possiblity, because it has to be possible to order the query by ANY of the specifications belonging of each product.

  • 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-25T15:21:07+00:00Added an answer on May 25, 2026 at 3:21 pm

    Your query seems correct. It will return all products’ details for those products that match your criteria (they are realted to all (<names of specs>) and all (<content of specs>)). It would be better to use SELECT p.* by the way, since you are grouping by p.products_id. The results you get from the other columns are not really useful.

    Now, if you want to get these products and in addition, get all specifications for these products (not only the spcs that are in your two lists), use this:

    (update: Added GROUP BY pd.products_id and GROUP_CONCAT() to gather info for a product from many rows into one)

    SELECT pd.*
         , GROUP_CONCAT( ps.content
                         ORDER BY ps.spec_id )
         , GROUP_CONCAT( s.spec_name 
                         ORDER BY ps.spec_id )
         , GROUP_CONCAT( CONCAT(s.spec_name,'-',ps.content)
                         ORDER BY ps.spec_id )
    FROM
        ( SELECT p.*
          FROM products p
          JOIN products_specifications ps ON ps.products_id = p.products_id
          JOIN specifications s ON s.spec_id = ps.spec_id
          WHERE s.spec_name IN (<names of specs>)
          AND ps.content IN (<content of specs>)
          GROUP BY p.products_id
          HAVING COUNT(DISTINCT s.specifications_name) = <count of specs name>
          AND COUNT(DISTINCT ps.content) = <count of specs content>
        ) AS pd
      JOIN
        products_specifications ps ON ps.products_id = pd.products_id
      JOIN
        specifications s ON s.spec_id = ps.spec_id  
    GROUP BY pd.products_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.