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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:59:09+00:00 2026-05-23T22:59:09+00:00

sql select in relation many-to-many not works as expected i have the following tables

  • 0

sql select in relation many-to-many not works as expected

i have the following tables in relation many-to-many:

table product:
prd_cod (pk)
cat_cod (fk)
prd_nome 

table description_characteristic:
prd_cod(fk)
id_characteristic(fk)
description

table characteristic:
id_characteristic(pk)
name

we suppose that the cat_cod will be 1, so i want to show all the products that have the category code equals 1,that will be provided dynamically in php for parameter …

I have done this select below to solve my problem:

select p.prd_cod,p.prd_name,c.name_characteristic,dc.description
from product p,description_characteristic dc, characteristic c
where p.prd_cod = dc.prd_cod and
dc.id_ccharacteristic = c.id_characteristic and
p.cat_cod = 1

but the data were shown this way:

Prd_cod  Prd_name   name_characteristic  descript  
  1          pen        Color            pink      
  1          Pen        manufacturer     kingston  
  1          Pen        type                 brush
  1          Pen        weight               0.020

I want to show the result this way:

Prd_cod  Prd_name   name_characteristic  descript  name_characteristic  descript
  1          pen        Color            pink      type                 brush
  2          Pen-drive  manufacturer     kingston  weight               0.020

I would like to show all the characteristics of the same product, and not just two as above…

I can not do a select to solve this
please i need help
Thank you all

  • 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-23T22:59:09+00:00Added an answer on May 23, 2026 at 10:59 pm

    As stated by Tom H., your database layout may generally be a bad idea and you might reconsider it. That said, there is no really clean solution producing the mysql result set you want. But you might use something crazy with GROUP_CONCAT like this, reproducing the real data with some PHP-side postprocessing:

    SELECT p.prd_cod, p.prd_name, GROUP_CONCAT(c.name_characteristic), GROUP_CONCAT(dc.description)
    from product p,description_characteristic dc, characteristic c
    where p.prd_cod = dc.prd_cod and
    dc.id_ccharacteristic = c.id_characteristic and
    p.cat_cod = 1
    GROUP BY p.prd_cod
    

    This should return something like

    Prd_cod  Prd_name   name_characteristic               descript  
      1          pen        Color,manufacturer            pink,Apple
    

    this result set should be post-processed like this:

    $out = array();
    foreach ($result as $res) {
        $p = array_combine(
                 explode(",", $res["name_characteristic"]),
                 explode(",", $res["descript"]));
        $p["prd_cod"] = $res["prd_cod"];
        $out[] = $p;
    }
    

    I would not call this a robust solution, though – for example, commas in your values screw things up. If you want to avoid madness like this, you’ll have to postprocess the result you already get.

    But if you want to show some mad sql aggregation skills (and you seem to be interested as you’ve chosen this database layout), this one is for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SQL query which select the record from three tables and there is no relation
I have the following SQL: SELECT * FROM [Database].dbo.[TagsPerItem] INNER JOIN [Database].dbo.[Tag] ON [Tag].Id
I have a many to many table relation ship: CUS_Phone : holds its own
I am trying to get a One-To-Many relation working. I have the following mappings:
I have a many-to-many relation with my database, so I created a sql query
SQL: select * from user where room_id not in (select id from rooms); what
I have a simple SQL 'Select' query, and I'd like to dump the results
The following SQL: SELECT notes + 'SomeText' FROM NotesTable a Give the error: The
The following SQL SELECT * FROM customers converted to this in LINQ var customers
I have a SQL SELECT query which has a LIKE clause containing an underscore,

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.