Solved

How update multi-value field in MySQL?

Posted on 2014-01-01
9
422 Views
Last Modified: 2014-01-08
Hi and Happy New Year!

I have 3 tables:

Table1
  Row1:
    Col1: Jack
    Col2: code1,code3
  Row2:
    Col1: Sam
    Col2: code1,code2,code3
  Row3:
    Col1: Jeff
    Col2: code2

Table2
  Row1:
    Col1: code1
    Col2: desc1
  Row2:
    Col1: code2
    Col2: desc2
  Row3:
    Col1: code3
    Col2: desc3

I want to create Table3:

Table3
  Row1:
    Col1: Jack
    Col2: desc1\r\ndesc3
  Row2:
    Col1: Sam
    Col2: desc1\r\ndesc2\r\ndesc3
  Row3:
    Col1: Jeff
    Col2: desc2

Table1 col2 can have between 0 and 3 of the codes (comma separated) from Table2 in any order
I want Table3 col2 to have a separate line per code (i.e. up to 3 lines using \r\n) instead of comma separated

Would sure appreciate someone putting together the insert statement I need.  Thanks--
Sam
0
Comment
Question by:SAbboushi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 39750418
I think you need to try to normalise your data. Instead of having comma-separated list of codes in your table you need to separate the different codes into different rows, like this:
Table1
  Row1:
    ID
    Col1: Jack
    Col2: code1
  Row2:
    ID
    Col1: Jack
    Col2: code3
  Row3:
    ID
    Col1: Sam
    Col2: code1
  Row4:
    ID
    Col1: Sam
    Col2: code2
  Row5:
    ID
    Col1: Sam
    Col2: code3
  Row6:
    Col1: Jeff
    Col2: code2

Open in new window

This way the data will be more manageable.

Another note: You do not need to create an extra table Table3 to store descriptions. The descriptions can always be retrieved using a query

Please let us know if you are willing to follow this advice and we can help you with the query for the descriptions from the normalised data.

Otherwise, the task you have requested is also doable using queries.
0
 

Author Comment

by:SAbboushi
ID: 39750439
Thanks for the suggestions - If it were my design, I would prefer normalized.

Need to solve the problem though as outlined.
0
 

Author Comment

by:SAbboushi
ID: 39750504
Anyone?
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 25

Accepted Solution

by:
chaau earned 500 total points
ID: 39750609
OK, if you are stuck with this design, the query as simple as this will help you:
create table table1(person varchar(10), codes varchar(30));
create table table2(code varchar(10), descr varchar(10));
insert into table1 values('Jack', 'code1,code2'),('Mary', 'code1'),('Michael', 'code1,code2,code3');
insert into table2 values('code1','desc1'),('code2','desc2'),('code3','desc3');

select t1.person, group_concat(t2.descr ORDER BY t2.descr SEPARATOR '\r\n')
from table1 t1 inner join table2 t2 on
find_in_set(t2.code,t1.codes) > 0
group by t1.person

Open in new window

0
 

Author Comment

by:SAbboushi
ID: 39750619
Thanks - almost perfect... don't understand why 2nd & 3rd lines appear indented e.g.
desc1
 desc2
0
 
LVL 25

Expert Comment

by:chaau
ID: 39750623
They are not intended here. Are you sure you used '\r\n' as a separator, not '\r\n '
0
 

Author Comment

by:SAbboushi
ID: 39750673
I copied and pasted your script...

How do I configure SQL Fiddle to show the results on multiple lines (i.e. to render the \r\n?)

I am using Workbench
0
 
LVL 25

Expert Comment

by:chaau
ID: 39750686
There is a dropdown list next to "Run SQL" button. Select "plaintext output".
However, I think I found the problem. I believe MySQL uses unix style Carriage Returns. So, in this case modify the SEPARATOR to use '\n'
0
 

Author Closing Comment

by:SAbboushi
ID: 39765759
Thanks so much for you help!
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
insert into database through form with dynamic fields. 2 59
SubQuery link 4 59
MYSQL responding very slow 3 67
How can I use javascript variable in mysql query 21 46
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question