如何在sql中使用group concat連接三個表

[英]How to join three tables with group concat in sql


I am trying to join three tables with group concat.Problem is main tables have joining column in json format.

我正在嘗試使用group concat連接三個表.Problem是主表以json格式加入列。

Query is working but repeating the joining column data again and again query ->

查詢正在運行,但一次又一次地重復加入列數據查詢 - >

[SELECT tour_package.description AS description, 
         tour_package.NAME AS NAME, 
         GROUP_CONCAT( destination_continent.NAME ) AS continent_name,
         GROUP_CONCAT( travel_style.NAME ) AS travel_style_name,
         tour_package.id AS id, 
         tour_package.img_path_thumb AS img_path_thumb, 
         tour_package.continent_id, 
         tour_package.travel_style_id
FROM tour_package
LEFT JOIN destination_continent ON 
      FIND_IN_SET( destination_continent.id, 
           REPLACE( REPLACE( REPLACE( tour_package.continent_id,  '\[',  '' ) ,  '\]',  '' ) ,  '\"',  '' ) ) 
LEFT JOIN travel_style ON 
      FIND_IN_SET( travel_style.id, 
           REPLACE( REPLACE( REPLACE( tour_package.travel_style_id,  '\[',  '' ) ,  '\]',  '' ) ,  '\"',  '' ) ) 
WHERE  `tour_package`.`DELETE` = 0 &&  `destination_continent`.`DELETE` = 0
LIMIT 0 , 30][1]

Tables ->image1

表 - >

image2

image3

2 个解决方案

#1


0  

Use DISTINCT keyword in GROUP_CONCAT function

在GROUP_CONCAT函數中使用DISTINCT關鍵字

I.e:

即:

SELECT tour_package.description AS description, 
       tour_package.NAME AS NAME, 
       GROUP_CONCAT(DISTINCT destination_continent.NAME ) AS continent_name,
       GROUP_CONCAT(DISTINCT travel_style.NAME ) AS travel_style_name,
       tour_package.id AS id, 
       tour_package.img_path_thumb AS img_path_thumb, 
       tour_package.continent_id, 
       tour_package.travel_style_id
.... 

#2


0  

While Group Concatenating do a Distinct check

Group Concatenating進行不同的檢查

[SELECT tour_package.description AS description, tour_package.NAME AS NAME, GROUP_CONCAT( DISTINCT(destination_continent.NAME) ) AS continent_name, GROUP_CONCAT( DISTINCT(travel_style.NAME) ) AS travel_style_name, tour_package.id AS id, tour_package.img_path_thumb AS img_path_thumb, tour_package.continent_id, tour_package.travel_style_id FROM tour_package LEFT JOIN destination_continent ON FIND_IN_SET( destination_continent.id, REPLACE( REPLACE( REPLACE( tour_package.continent_id,  '\[',  '' ) ,  '\]',  '' ) ,  '\"',  '' ) )  LEFT JOIN travel_style ON FIND_IN_SET( travel_style.id, REPLACE( REPLACE( REPLACE( tour_package.travel_style_id,  '\[',  '' ) ,  '\]',  '' ) ,  '\"',  '' ) )  WHERE  `tour_package`.`DELETE` =0 &&  `destination_continent`.`DELETE` =0 LIMIT 0 , 30][1]

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/05/06/1a0ebcdb11c704aa0dfcf6eeaac2d2f6.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com