MySQL takes out the comma-separated values ​​from a field to form a new field

MySQL takes out the comma-separated values ​​from a field to form a new field

1For example: To split the fields shown in Figure 1 into Figure 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: Then connect with the shops table to query and extract the fields we need. Here I need to extract 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: Combine the same account_id into one line, separated by commas

//These two are examples from the Internet 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;
//With the help of the above two references 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

The effect is as follows

Second method

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] -- The storage format of this field is 7,8,9,11,10,12, and the data type is 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

result

This is the end of this article about how to extract comma-separated values ​​from a field in MySQL to form a new field. For more information about comma-separated values ​​in MySQL fields, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Two methods of MySql comma concatenation string query
  • Mysql query statement to get the first data of the comma in the field value
  • How to use comma delimiters in MySQL fields
  • How to convert a column of comma-separated values ​​into columns in MySQL

<<:  Implementation of fastdfs+nginx cluster construction

>>:  Detailed explanation of built-in methods of javascript array

Recommend

A brief discussion of 12 classic problems in Angular

Table of contents 1. Please explain what are the ...

Three.js sample code for implementing dewdrop animation effect

Preface Hello everyone, this is the CSS wizard - ...

Solution to ElementUI's this.$notify.close() call not working

Table of contents Requirement Description Problem...

A link refresh page and js refresh page usage examples

1. How to use the link: Copy code The code is as f...

Analysis of the Neglected DOCTYPE Description

doctype is one of them: <!DOCTYPE HTML PUBLIC &...

MySQL InnoDB row_id boundary overflow verification method steps

background I talked to my classmates about a boun...

How to use CURRENT_TIMESTAMP in MySQL

Table of contents Use of CURRENT_TIMESTAMP timest...

Example code of vue + element ui to realize player function

The display without the effect picture is just em...

New settings for text and fonts in CSS3

Text Shadow text-shadow: horizontal offset vertic...

Summary of common optimization operations of MySQL database (experience sharing)

Preface For a data-centric application, the quali...

How to add shortcut commands in Xshell

As a useful terminal emulator, Xshell is often us...

Detailed explanation of MySQL partition table

Preface: Partitioning is a table design pattern. ...

Notes on using the blockquote tag

<br />Semanticization cannot be explained in...

JavaScript to achieve progress bar effect

This article example shares the specific code of ...