Solved

How update multi-value field in MySQL?

Posted on 2014-01-01
9
419 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
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 24

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 24

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 24

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

810 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