濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > MySQL將一個(gè)字段中以逗號(hào)分隔的取出來形成新的字段實(shí)現(xiàn)

MySQL將一個(gè)字段中以逗號(hào)分隔的取出來形成新的字段實(shí)現(xiàn)

熱門標(biāo)簽:芒果電話機(jī)器人自動(dòng)化 石家莊電商外呼系統(tǒng) 日照旅游地圖標(biāo)注 信陽穩(wěn)定外呼系統(tǒng)運(yùn)營商 廣東人工電話機(jī)器人 南通自動(dòng)外呼系統(tǒng)軟件 申請(qǐng)外呼電話線路 百度地圖圖標(biāo)標(biāo)注中心 湖南人工外呼系統(tǒng)多少錢

1例如:要把如圖1的字段拆分圖2

select account_id,
    substring_index(substring_index(a.related_shop_ids,','
    ,b.help_topic_id+1),',',-1) shopid
  from 
    sales_hang_account a
  join
    mysql.help_topic b
    on b.help_topic_id  (length(a.related_shop_ids) -   length(replace(a.related_shop_ids,',',''))+1) 
  order by a.account_id

2:然后和shops表進(jìn)行連接查詢?nèi)〕鑫覀冃枰淖侄?,我這里需要取出name

select s.`name` as shopname,a.account_id from shops s
inner JOIN (

select account_id,
    substring_index(substring_index(a.related_shop_ids,','
    ,b.help_topic_id+1),',',-1) shopid
  from 
    sales_hang_account a
  join
    mysql.help_topic b
    on b.help_topic_id  (length(a.related_shop_ids) -   length(replace(a.related_shop_ids,',',''))+1) 
  order by a.account_id)a on s.shop_id=a.shopid

3:將 account_id相同合并成一行,以逗號(hào)隔開

//這兩個(gè)是網(wǎng)上的例子
select ID,group_concat(NAME) as NAME from table group by ID;

select ID,group_concat(NAME SEPARATOR ';') as NAME from a group by ID;
//借助上面兩個(gè)參考
select account_id,GROUP_CONCAT(shopname SEPARATOR',')as shopname from (select s.`name` as shopname,a.account_id from shops s
inner JOIN (

select account_id,
    substring_index(substring_index(a.related_shop_ids,','
    ,b.help_topic_id+1),',',-1) shopid
  from 
    sales_hang_account a
  join
    mysql.help_topic b
    on b.help_topic_id  (length(a.related_shop_ids) -   length(replace(a.related_shop_ids,',',''))+1) 
  order by a.account_id)a on s.shop_id=a.shopid) a GROUP BY account_id

效果如下

第二種方法

select g.account_id,g.related_shop_ids,GROUP_CONCAT(s.name)as shopname from sales_hang_account g left join shops s on FIND_IN_SET(s.shop_id , g.related_shop_ids)
GROUP BY g.account_id

SqlServer

 [Product] -- 該字段存儲(chǔ)格式為 7,8,9,11,10,12 ,數(shù)據(jù)類型為nvarchar
select id,ProductName=stuff((select ',' + product_chinaname from base_supplier_product where charindex(','+ltrim(productid)+',',','+ Product + ',') > 0 for xml path('') ), 1, 1, '')
 FROM base_Pre_sale_project

結(jié)果

到此這篇關(guān)于MySQL將一個(gè)字段中以逗號(hào)分隔的取出來形成新的字段實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MySQL字段逗號(hào)分隔內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • MySql逗號(hào)拼接字符串查詢的兩種方法
  • Mysql 取字段值逗號(hào)第一個(gè)數(shù)據(jù)的查詢語句
  • 在MySQL字段中使用逗號(hào)分隔符的方法分享
  • MySQL中將一列以逗號(hào)分隔的值行轉(zhuǎn)列的實(shí)現(xiàn)

標(biāo)簽:惠州 阿里 沈陽 牡丹江 合肥 公主嶺 呼和浩特 天津

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL將一個(gè)字段中以逗號(hào)分隔的取出來形成新的字段實(shí)現(xiàn)》,本文關(guān)鍵詞  MySQL,將,一個(gè),字段,中,以,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySQL將一個(gè)字段中以逗號(hào)分隔的取出來形成新的字段實(shí)現(xiàn)》相關(guān)的同類信息!
  • 本頁收集關(guān)于MySQL將一個(gè)字段中以逗號(hào)分隔的取出來形成新的字段實(shí)現(xiàn)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    浏阳市| 沙田区| 宝兴县| 泾川县| 临清市| 信宜市| 黔西| 剑阁县| 巩义市| 神木县| 麻城市| 双城市| 福泉市| 北辰区| 龙陵县| 新郑市| 祁阳县| 炉霍县| 农安县| 隆昌县| 永康市| 青冈县| 绿春县| 建瓯市| 同仁县| 桂平市| 桦南县| 三台县| 布尔津县| 永寿县| 台东市| 湾仔区| 濉溪县| 贞丰县| 贡嘎县| 民乐县| 九寨沟县| 曲沃县| 南澳县| 米泉市| 工布江达县|