Advertisement

05.15.2008 at 09:52AM PDT, ID: 23405877
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.0

How to separate comma delimited values from a single column into multiple columns?

Asked by avfcreations in MySQL Server, SQL Query Syntax, WebApplications

Tags:

I'm working with a MySQL 5 db that uses 2 tables to store records - one records table that has a record ID for each submission and a subrecords table that contains name/value pairs for each of the elements and a record number linking them to the header record.  This means that each record I want to display (for example, "Name, "Phone, "Address") is stored in multiple lines in the subrecords table with a matching record number that links them together.

I've been able to create a view that puts all related subrecords values into a single field by record number -

SELECT record, GROUP_CONCAT(value ORDER BY value separator ',') AS vals FROM subrecords  GROUP BY record

This returns two columns, like -

Record      vals
947      yes,Stage 1,St. Joseph,No Preference,,,,,,,,,,,,,,,,,,,,Russell,Tripp,TESTING,,TEST,SC,29607,8888888888,work,JUSTTESTING@avfcreations.com,large,jklw;erjio,8888888888,Yes

Now I need a decent method of getting each of these values into a separate column from the view results,  I've experimented some with using SUBSTRING_INDEX() and other string functions to get each value, but after you get past the first couple of values, my method for extracting becomes incredibly long and difficult to figure out what's going on - I feel certain there must be a better way to do this.  There may or may not be values in each delimited field, but the number of fields is constant.  Here's what I did to get the first few values successfully (the view above is named "build" here) -

SELECT

b.id AS RecordNum,

SUBSTRING_INDEX(b.vals,',',1) AS ReadDesc,
SUBSTRING(SUBSTRING_INDEX(b.vals,',',2) FROM (LOCATE(',',SUBSTRING_INDEX(b.vals,',',2))+1)) AS Stage1,

SUBSTRING(SUBSTRING_INDEX(b.vals,',',3) FROM (LOCATE(',',SUBSTRING_INDEX(b.vals,',',3),( (LOCATE(',',SUBSTRING_INDEX(b.vals,',',2))+1))))+1) AS Stage1Loc,

SUBSTRING(SUBSTRING_INDEX(b.vals,',',4) FROM (LOCATE(',',SUBSTRING_INDEX(b.vals,',',4),( (LOCATE(',',SUBSTRING_INDEX(b.vals,',',4))+ LENGTH(SUBSTRING(SUBSTRING_INDEX(b.vals,',',3) FROM (LOCATE(',',SUBSTRING_INDEX(b.vals,',',3),( (LOCATE(',',SUBSTRING_INDEX(b.vals,',',2))+1))))+1))))))+1) AS Stage1Pos

FROM build b

It works, but as you can see if I keep going to get additional records out, the lines in the select statement are going to be unbelievably long and, I'm sure, not very efficient.  I need to find a way to do this in MySQL 5.  Thanks.Start Free Trial
[+][-]05.15.2008 at 10:14AM PDT, ID: 21575981

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.15.2008 at 10:31AM PDT, ID: 21576174

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.15.2008 at 10:34AM PDT, ID: 21576200

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.15.2008 at 10:43AM PDT, ID: 21576309

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.15.2008 at 11:59AM PDT, ID: 21576920

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: MySQL Server, SQL Query Syntax, WebApplications
Tags: SQL, MySQL 5
Sign Up Now!
Solution Provided By: rickerman
Participating Experts: 2
Solution Grade: A
 
 
[+][-]05.15.2008 at 12:25PM PDT, ID: 21577147

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080924-EE-VQP-38 / EE_QW_2_20070628