Solved

How update multi-value field in MySQL?

Posted on 2014-01-01
9
421 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 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

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.

Question has a verified solution.

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

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

740 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