Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql查询的重复结果_Mysql - Fatal编程技术网

mysql查询的重复结果

mysql查询的重复结果,mysql,Mysql,执行查询后,在尝试获取树结构的类别id时,我们将获得重复的id。 我们期待一个没有重复id的输出 SELECT GROUP_CONCAT(CONCAT_WS(',',t1.CategoryID,t2.CategoryID,t3.`CategoryID`, t4.CategoryID)) AS id FROM prodcategory AS t1 LEFT JOIN prodcategory AS t2 ON t2.ParentCategoryID = t1.CategoryID LE

执行查询后,在尝试获取树结构的类别id时,我们将获得重复的id。 我们期待一个没有重复id的输出

SELECT 
  GROUP_CONCAT(CONCAT_WS(',',t1.CategoryID,t2.CategoryID,t3.`CategoryID`, t4.CategoryID)) AS id 
FROM prodcategory AS t1 
LEFT JOIN prodcategory AS t2 ON t2.ParentCategoryID = t1.CategoryID 
LEFT JOIN prodcategory AS t3 ON t3.ParentCategoryID = t2.CategoryID 
LEFT JOIN prodcategory AS t4 ON t4.ParentCategoryID=t3.CategoryID 
WHERE t1.ParentCategoryID =41
实际产量:

190,256,190,257,191,261,300,191,262,192,267
190,256,257,191,261,300,262,192,267
预期产出:

190,256,190,257,191,261,300,191,262,192,267
190,256,257,191,261,300,262,192,267
示例数据库数据:

CategoryID |  CategoryName |  ParentCategoryID
   41      |       a       |        1
   190     |       b       |        41
   191     |       c       |        41
   192     |       d       |        41
   256     |       e       |        190
   257     |       f       |        190
   261     |       g       |        191
   262     |       h       |        191
   300     |       i       |        261
   267     |       j       |        192 

对于重复的结果,可以使用不同的关键字

    SELECT GROUP_CONCAT(CONCAT_WS(',',DISTINCT 
    t1.CategoryID,DISTINCT t2.CategoryID,DISTINCT t3.`CategoryID`,DISTINCT  
    t4.CategoryID)) AS id FROM prodcategory AS t1 
    LEFT JOIN prodcategory AS t2 ON t2.ParentCategoryID = t1.CategoryID 
    LEFT JOIN prodcategory AS t3 ON t3.ParentCategoryID = t2.CategoryID 
    LEFT JOIN prodcategory AS t4 ON t4.ParentCategoryID=t3.CategoryID 
    WHERE t1.ParentCategoryID =41

附加与您正在生成的输出相匹配的样本数据。
选择GROUP_CONCAT(不同的CONCAT_WS(..
有帮助吗?@Arulkumar仍然存在重复项。@KamilGosciminski您需要带数据的示例表结构?@SüreshAK是的,请阅读有关提供MVCE的更多信息:结果中仍然存在重复项使用数据发送给我您的数据库转储??dhanushkasasankapayza@gmail.comadding引发语法错误的每列中都不相同