If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > How to populate a new table with data from another?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-19-08, 04:46
nickweavers nickweavers is offline
Registered User
 
Join Date: Jan 2005
Posts: 15
How to populate a new table with data from another?

Hi,

I have a table that contains, amoung other things, peoples names (as a string "lastname,firstname"). I would like to normalise this by separating the names out into another table with seperate fields for firstname and lastname, and at the same time replace the names in the first table with an id that references the name in the new table.

Can this be done in a single query?

TIA,
Nick

Last edited by nickweavers; 02-19-08 at 04:57.
Reply With Quote
  #2 (permalink)  
Old 02-19-08, 05:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
not in a single query, no
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-19-08, 05:46
nickweavers nickweavers is offline
Registered User
 
Join Date: Jan 2005
Posts: 15
Thanks.

I am trying to do it as two operations. The first is to copy data from the first table into the second:

INSERT INTO `persons` ( '', lastName, firstName )
SELECT DISTINCT SUBSTRING_INDEX( owner, ',' , -1 ) AS firstName, SUBSTRING_INDEX( owner, ',' , +1 ) AS lastName
FROM `machinestracker`;

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''',lastName, firstName)
SELECT DISTINCT SUBSTRING_INDEX(owner,',',-1) AS fir' at line 1

I can't understand what it is indicating as the syntax error. Can anyone help?

I used the following example query which is given on the MySQL website http://dev.mysql.com/doc/refman/5.0/...rt-select.html and added the extra stuff to split the owner field on the comma that delimits the owners last and first names.

INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

Last edited by nickweavers; 02-19-08 at 07:40.
Reply With Quote
  #4 (permalink)  
Old 02-19-08, 08:00
nickweavers nickweavers is offline
Registered User
 
Join Date: Jan 2005
Posts: 15
Spotted it... I was getting confused between values and names in the field list for the INSERT and had "INSERT persons ('', lastName, firstName)" thinking I needed to assign '' to the auto-incrementing ID field of persons.

This now works:

INSERT persons (lastName, firstName)
SELECT DISTINCT SUBSTRING_INDEX(machinestracker.owner,',',-1) AS firstName, SUBSTRING_INDEX(machinestracker.owner,',',+1) AS lastName FROM machinestracker

Thx
Reply With Quote
  #5 (permalink)  
Old 02-19-08, 08:58
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
nice one

not too many people know about or appreciate the beautiful SUBSTRING_INDEX function



also, next time, please post in the mysql forum

this question wasn't really about "Database Concepts & Design"

ta
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On