[英]How to join three tables with group concat in sql
我正在嘗試使用group concat連接三個表.Problem是主表以json格式加入列。
查詢正在運行,但一次又一次地重復加入列數據查詢 - >
[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]
在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
....
Group Concatenating進行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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.