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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:20:35+00:00 2026-05-14T15:20:35+00:00

Hi i’m struggling to write a particular MySQL Join Query. I have a table

  • 0

Hi i’m struggling to write a particular MySQL Join Query.

I have a table containing product data, each product can belong to multiple categories. This m:m relationship is satisfied using a link table.

For this particular query I wish to retrieve all products belonging to a given category, but with each product record, I also want to return the other categories that product belongs to.

Ideally I would like to achieve this using an Inner Join on the categories table, rather than performing an additional query for each product record, which would be quite inefficient.

My simplifed schema is designed roughly as follows:

products table:

product_id, name, title, description, is_active, date_added, publish_date, etc....

categories table:

category_id, name, title, description, etc...

product_category table:

product_id, category_id

I have written the following query, which allows me to retrieve all the products belonging to the specified category_id. However, i’m really struggling to work out how to retrieve the other categories a product belongs to.

SELECT p.product_id, p.name, p.title, p.description
FROM prod_products AS p
LEFT JOIN prod_product_category AS pc
ON pc.product_id =  p.product_id
WHERE pc.category_id = $category_id
AND UNIX_TIMESTAMP(p.publish_date) < UNIX_TIMESTAMP()
AND p.is_active = 1
ORDER BY p.name ASC

I’d be happy just retrieving the category id’s releated to each returned product row, as I will have all category data stored in an object, and my application code can take care of the rest.

Many thanks,

Richard

  • 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-14T15:20:36+00:00Added an answer on May 14, 2026 at 3:20 pm
    SELECT p.product_id, p.name, p.title, p.description, 
      GROUP_CONCAT(otherc.category_id) AS other_categories
    FROM prod_products AS p
    JOIN prod_product_category AS pc
      ON pc.product_id =  p.product_id
    LEFT JOIN prod_product_category AS otherc
      ON otherc.product_id = p.product_id AND otherc.category_id != pc.category_id
    WHERE pc.category_id = $category_id
      AND UNIX_TIMESTAMP(p.publish_date) < UNIX_TIMESTAMP()
      AND p.is_active = 1
    GROUP BY p.product_id
    ORDER BY p.name ASC
    
    • 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.