如何在一对多关系中避免重复键连接表?

时间:2017-10-04 00:13:06

标签: php mysql join

我有6个表,我试图查询,但我在查询结果中获得重复的主键。以下是表格:

userarticles

id nomarticles 
 1 escarpin_estelle
 2 Tallon_grace

articlesize

productid  idsizes
 1          1
 1          2
 2          3
 2          5

尺寸

idsizes size
  1       36
  2       38
  3       40
  4       44
  5       32

articlematieres

id_article id_matiere
  1          2
  1          1
  2          3
  2          3

matiere

matiereid  matierename
 1          daim
 2          coton
 3          polyster

article_imgs

imageid articleID filenames
  1       1       rouge2017-10-03.jpg   
  2       1       2017-10-03-204220.jpg
  3       2       moulante201.jpg
  4       2       avec-decollete.jpg

希望的结果应该是

id  articlename        size   filenames                            matiere 
 1  escarpin_estelle   38,45  rouge2017-10-03.jpg,moulante201.jpg  coton,daim

这是我的查询:

SELECT id,nomarticle,filenames,size,matierename FROM userarticles AS products LEFT JOIN article_imgs AS images ON images.articleID = products.id LEFT JOIN articlesize AS prodt ON prodt.idarticle = products.id LEFT JOIN sizes AS s ON s.idsizes = prodt.idsize LEFT JOIN articlematieres AS prodmat ON prodmat.id_article = products.id LEFT JOIN matiere AS m ON m.matiereid = prodmat.id_matiere WHERE products.id = 1

1 个答案:

答案 0 :(得分:0)

真的确实有多个答案:

products.id = 1有2张图片,2张尺寸和2张matiere

除非您在WHERE

中指定更多字段,否则无法获得唯一答案

** --- OPTIONALLY --- **

您可以在查询中添加LIMIT 1以获取第一个,但这可能是错误的。