Mysql

MySQL用逗號分隔值連接兩個表

  • December 28, 2020

我有兩個表如下:

卡琳

Datimarketing(是前面列的組連接的結果)

我希望查詢我的 Carline 表並將“option_name”列與 Datimarketing 表中的值相關聯。

輸出應該是:

這可以通過 CONCAT 或 FIND_IN_SET 來完成嗎?

請幫忙!提前致謝!

SELECT GROUP_CONCAT(DISTINCT c0.option_name) Hobby,
      GROUP_CONCAT(DISTINCT c1.option_name) CarType,
      GROUP_CONCAT(DISTINCT c2.option_name) Datimarketing
FROM Datimarketing d
JOIN carline c0 ON FIND_IN_SET(c0.option_value, d.Hobby)
JOIN carline c1 ON FIND_IN_SET(c1.option_value, d.CarType)
JOIN carline c2 ON FIND_IN_SET(c2.option_value, d.Datimarketing)
GROUP BY d.Hobby, d.CarType, d.Datimarketing;

小提琴

注意 - 逗號後沒有空格字元。如果它們存在,則根據 REGEXP_REPLACE 在連接條件下修剪它們。

   SELECT GROUP_CONCAT(DISTINCT c0.option_name)segmento_auto_richiesto,
      GROUP_CONCAT(DISTINCT c1.option_name)alimentazione,
      GROUP_CONCAT(DISTINCT c2.option_name)km_annui_percorsi,
      GROUP_CONCAT(DISTINCT c3.option_name)modalit_di_acquisto,    
      GROUP_CONCAT(DISTINCT c4.option_name)budget_di_spesa,
      GROUP_CONCAT(DISTINCT c5.option_name)rata_mensile,
      GROUP_CONCAT(DISTINCT c6.option_name)abitudini_di_acquisto,
      GROUP_CONCAT(DISTINCT c7.option_name)giorno_preferito_per_il_ricontatto,
      GROUP_CONCAT(DISTINCT c8.option_name)orario_preferito_per_il_ricontatto,
      GROUP_CONCAT(DISTINCT c9.option_name)datimarketing,
      GROUP_CONCAT(DISTINCT c10.option_name)privacy_mkt_all,
      GROUP_CONCAT(DISTINCT c11.option_name)numero_di_componenti_del_nucleo_familiare,
      GROUP_CONCAT(DISTINCT c12.option_name)hobby,
      GROUP_CONCAT(DISTINCT c13.option_name)professione,
      GROUP_CONCAT(DISTINCT c14.option_name)iscritto_ad_associazioni_di_categoria, 
FROM 1459630_form_1 d
JOIN 1459630_field_options c0 ON FIND_IN_SET(c0.option_value, d.segmento_auto_richiesto)
JOIN 1459630_field_options c1 ON FIND_IN_SET(c1.option_value, d.alimentazione)
JOIN 1459630_field_options c2 ON FIND_IN_SET(c2.option_value, d.km_annui_percorsi)
JOIN 1459630_field_options c3 ON FIND_IN_SET(c3.option_value, d.modalit_di_acquisto)    
JOIN 1459630_field_options c4 ON FIND_IN_SET(c4.option_value, d.budget_di_spesa)
JOIN 1459630_field_options c5 ON FIND_IN_SET(c5.option_value, d.rata_mensile)
JOIN 1459630_field_options c6 ON FIND_IN_SET(c6.option_value, d.abitudini_di_acquisto)
JOIN 1459630_field_options c7 ON FIND_IN_SET(c7.option_value, d.giorno_preferito_per_il_ricontatto)
JOIN 1459630_field_options c8 ON FIND_IN_SET(c8.option_value, d.orario_preferito_per_il_ricontatto)
JOIN 1459630_field_options c9 ON FIND_IN_SET(c9.option_value, d.datimarketing)
JOIN 1459630_field_options c10 ON FIND_IN_SET(c10.option_value, d.privacy_mkt_all)
JOIN 1459630_field_options c11 ON FIND_IN_SET(c11.option_value, d.numero_di_componenti_del_nucleo_familiare)
JOIN 1459630_field_options c12 ON FIND_IN_SET(c12.option_value, d.hobby)
JOIN 1459630_field_options c13 ON FIND_IN_SET(c13.option_value, d.professione)
JOIN 1459630_field_options c14 ON FIND_IN_SET(c14.option_value, d.iscritto_ad_associazioni_di_categoria)    

GROUP BY d.segmento_auto_richiesto, d.alimentazione, d.km_annui_percorsi, d.modalit_di_acquisto, d.budget_di_spesa, d.rata_mensile, d.abitudini_di_acquisto, d.giorno_preferito_per_il_ricontatto, d.orario_preferito_per_il_ricontatto, d.datimarketing, d.privacy_mkt_all, d.numero_di_componenti_del_nucleo_familiare, d.hobby, d.professione,d.iscritto_ad_associazioni_di_categoria;   

引用自:https://dba.stackexchange.com/questions/282099