开发者

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, I would like to show data like this:

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  desc开发者_如何学Cript  
  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 can not do a select to solve this please i need help Thank you all


Select VALUES YOU WANT
FROM CATEGORIES
LEFT JOIN product ON
product.cat_cod=CATEGORIES.cat_cod
LEFT JOIN description_characteristic ON
product.prd_cod = description_characteristic.prd_cod
LEFT JOIN characteristic ON
description_characteristic.id_characteristic=characteristic.id_characteristic
WHERE CATEGORIES.cat_cod = "1";

You will need to put tablename.column to select fields which exist in more than one table.

(I assumed your categories table was called CATEGORIES)


This query should resolve your problem, it will come in the same column....

select 
    p.prd_cod
    , p.prd_name 
    , GROUP_CONCAT(c.name,dc.description) cd_list
from 
    product p
    ,description_characteristic dc
    ,characteristic c
where 
    p.prd_cod = dc.prd_cod and
    dc.id_characteristic = c.id_characteristic and
    p.cat_cod = 1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜