Solved

How update multi-value field in MySQL?

Posted on 2014-01-01
9
420 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Creating and Managing Databases with phpMyAdmin in cPanel.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 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