2016-05-06 131 views
1

我想連接三個表與組concat.Problem是主表以json格式連接列。如何使用組連接在sql中連接三個表

查詢是工作,但一次又一次地重複接合列數據 查詢 - >

[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] 

表 - >image1

image2

image3

回答

0

使用在GROUP_CONCAT DISTINCT關鍵字功能

即:

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 
.... 
+0

日Thnx,現在我笑我自己,我必須要爲內存補品 –

0

在組串聯做Distinct檢查

[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]