MySQL comma split field column career

Source: Internet
Author: User

Objective:

Because many business tables use a design pattern that violates the first paradigm for historical reasons or for performance reasons. That is, multiple attribute values are stored in the same column (see the following table for specific structures).

In this mode, the application often needs to divide the column by delimiter and get the result of the column change.

Table data:

Id Value
1 Tiny,small,big
2 Small,medium
3 Tiny,big

Expect to get results:

Id Value
1 Tiny
1 Small
1 Big
2 Small
2 Medium
3 Tiny
3 Big

Summarize:

The disadvantage of this approach is that we need a separate table with sequential numbers (here is incre_table). and the maximum value of a continuous series must be greater than the number of values that match the split.

For example, if a row of msize has 100 comma-separated values, then our incre_table needs to have at least 100 contiguous rows.

Of course, MySQL also has a ready-made list of contiguous numbers available. If mysql.help_topic:help_topic_id a total of 504 values, generally can meet the majority of demand.

Rewrite as follows:

#需要处理的表create table Tbl_name (ID int, msize varchar), insert into tbl_name values (1, ' tiny,small,big ') and insert into TBL _name values (2, ' small,medium '); insert into tbl_name values (3, ' tiny,big '); #SQLselect A.id,substring_index (substring_ Index (A.msize, ', ', b.help_topic_id+1), ', ', -1) from Tbl_name ajoinmysql.help_topic bon b.help_topic_id < (length ( A.msize)-Length (replace (a.msize, ', ', ')) +1) Order by a.ID;

MySQL comma split field column career

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.