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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:26:35+00:00 2026-05-15T01:26:35+00:00

i got three tables CATS id name —————————— 1 category1 2 category2 3 category3

  • 0

i got three tables

CATS
id           name
------------------------------
1            category1
2            category2
3            category3
4            category4


PRODUCT
id           name
------------------------------
1            product1
2            product2



ZW-CAT-PRODUCT
id_cats      id_product
------------------------------
1            1
3            1
4            2

now i want to get my products and their categories

product1 => category1,category3
product2 => category4

is there a way to get this array (or object or something) with one mysql query?
i tried a bit with JOINS, but it seems thats this is not exactly what i need, or?

currently i’m using 3 querys (i think thats too much).

any suggestions?

edit

and on the other way, what if i want to get ALL products of a specific category?
can this also be done in one query?

  • 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-15T01:26:36+00:00Added an answer on May 15, 2026 at 1:26 am

    You can use GROUP_CONCAT to get a separated list in your results.

    SELECT p.*, 
           GROUP_CONCAT(c.name SEPARATOR ',') as cats
    FROM PRODUCT p
    LEFT JOIN ZW-CAT-PRODUCT l
      ON l.id_product=p.id
    LEFT JOIN CATS c
      ON c.id=l.id_cats
    GROUP BY p.id
    

    So basically, this first does some joins to get all the data. If you were to replace the GROUP_CONCAT line with just c.name, you would see a row for each product_id/category pair. The GROUP BY tells it to group results based on product ID, and then GROUP_CONCAT(c.name..) is telling to it take all the different c.name values that occur in a group (so for each product ID, since you’re grouping by product ID) and concatenate those values into one string, using , as the separator.

    So to get all products for a each category in the same style, it would be like this,

    SELECT c.*, 
           GROUP_CONCAT(p.name SEPARATOR ',') as products
    FROM CATS c
    LEFT JOIN ZW-CAT-PRODUCT l
      ON l.id_cats=c.id
    LEFT JOIN PRODUCT p
      ON p.id=l.id_product
    GROUP BY c.id
    

    EDIT: To get just the product rows for a particular category (as requested in comment), it’s this.

    SELECT p.*
    FROM PRODUCT p
    LEFT JOIN ZW-CAT-PRODUCT l
      ON l.id_product=p.id
    LEFT JOIN CATS c
      ON c.id=l.id_cats
    WHERE c.name='xyz';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got three tables: CREATE TABLE credential_types ( id serial PRIMARY KEY, name varchar(32)
I've got a CAkePHP 1.2 site. I've got three related Models/tables: A Comment has
I've got a database with three tables: Books (with book details, PK is CopyID),
I've got my database set up with three tables - code, tags, and code_tags
I've got three tables; Auctions, Auction Bids and Users. The table structure looks something
Got myself in a funny situation: page has three tables. Using sortEnd, any time
I got three tables: Houses, Person and PersonHouseAssignments In my Houses -Table I got
if've got three tables in my MYSQL Database and want to connect two of
I got three tables objects id, field1, field2, ... groups id, title, color group_ref
I've got three tables posts tags posts_tags I need to write some controller logic

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.