Solved

How update multi-value field in MySQL?

Posted on 2014-01-01
9
413 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
  • 5
  • 4
9 Comments
 
LVL 24

Expert Comment

by:chaau
Comment Utility
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
Comment Utility
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
Comment Utility
Anyone?
0
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
Comment Utility
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

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

Expert Comment

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

Author Comment

by:SAbboushi
Comment Utility
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 24

Expert Comment

by:chaau
Comment Utility
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
Comment Utility
Thanks so much for you help!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now