MySQL splitting a string with a delimiter and taking the 1st value and update
Hey everyone,
I am trying to take a string like "Brown, James" sans quotes and taking the substring James and tossing it into the column first_name. Here is what I think would do it, but I know its not the right way because it doesn't work. insert into instructors (first_name) SELECT SUBSTRING_INDEX(instructor, ' , ', 1) FROM srs_data; Any help would be great, Ronnie |
I noticed in your code you actually have:
Code:
' , ' Can you post your mysql output? |
mmmh yeah accident.
+---------------+--------------+-----------+---------------+----------------+---------------+ | instructor_id | first_name | last_name | office_number | office_phone | instructor | +---------------+--------------+-----------+---------------+----------------+---------------+ | 1 | NULL | NULL | Office Num | OffiicePhone | Instructor | | 2 | NULL | NULL | 180/222 | (205) 740-7661 | Avery, Terry | | 3 | NULL | NULL | 180/226 | (205) 740-6116 | Dutchuk, Mark | | 4 | NULL | NULL | 180/227 | (205) 740-6777 | Croft, David | | 5 | NULL | NULL | 180/226 | (205) 740-6777 | Croft, David | | 6 | NULL | NULL | 180/224 | (205) 740-6777 | Croft, David | | 7 | NULL | NULL | 180/224 | (205) 740-6778 | Loewen, John | | 17 | Avery, Terry | NULL | NULL | NULL | NULL | | 16 | Avery, Terry | NULL | NULL | NULL | NULL | | 15 | Avery, Terry | NULL | NULL | NULL | NULL | +---------------+--------------+-----------+---------------+----------------+---------------+ Instructor_id 1-7 were there before as a test. 17 + are the result of that statement. |
Ok it will do the last_name like I want now, however I want it to update the table: I get a syntax error, so I think I am being too clever for MySQL.. php you can get away with murder ;)
update instuctors WHERE last_name = SELECT DISTINCT SUBSTRING_INDEX(instructor, ', ', 1) FROM instructors; |
Fixed part of it:
mysql> insert into instructors (last_name) SELECT SUBSTRING_INDEX(instructor, ', ', 1) FROM instructors; Query OK, 7 rows affected (0.00 sec) Records: 7 Duplicates: 0 Warnings: 0 mysql> select * from instructors; +---------------+------------+------------+---------------+----------------+---------------+ | instructor_id | first_name | last_name | office_number | office_phone | instructor | +---------------+------------+------------+---------------+----------------+---------------+ | 1 | NULL | NULL | Office Num | OffiicePhone | Instructor | | 2 | NULL | NULL | 180/222 | (205) 740-7661 | Avery, Terry | | 3 | NULL | NULL | 180/226 | (205) 740-6116 | Dutchuk, Mark | | 4 | NULL | NULL | 180/227 | (205) 740-6777 | Croft, David | | 5 | NULL | NULL | 180/226 | (205) 740-6777 | Croft, David | | 6 | NULL | NULL | 180/224 | (205) 740-6777 | Croft, David | | 7 | NULL | NULL | 180/224 | (205) 740-6778 | Loewen, John | | 331 | NULL | Loewen | NULL | NULL | NULL | | 330 | NULL | Croft | NULL | NULL | NULL | | 329 | NULL | Croft | NULL | NULL | NULL | | 328 | NULL | Croft | NULL | NULL | NULL | | 327 | NULL | Dutchuk | NULL | NULL | NULL | | 326 | NULL | Avery | NULL | NULL | NULL | | 325 | NULL | Instructor | NULL | NULL | NULL | +---------------+------------+------------+---------------+----------------+---------------+ 14 rows in set (0.00 sec) Its still not doing what I want though, any suggestions? |
1st, maybe wanna think about posting actual phone numbers on the net. This page will be archived and be around forever, maybe use some asterisks.
2nd, I have a suggestion, but make a copy of the data and work on that. I haven't used mysql in a long time, and I have no idea if this will work. What you're doing there is adding new rows, when it sounds like you need to be updating rows. Try: Code:
UPDATE instructors SET last_name=SUBSTRING_INDEX(instructor, ", ", 1), first_name=SUBSTRING_INDEX(instructor, ", ", -1) Edit ---- Also use code tags. |
Quote:
So one more quick question. I would trim the instructor column to get rid of the ", " and then to add a "@hotmail.com" I would use concatonation? |
Code:
UPDATE students SET email = LOWER(CONCAT(LEFT(SUBSTRING_INDEX(first_name, ", ", 1),1),SUBSTRING_INDEX(last_name, ", ", -1),"@shoes.ca")) WHERE first_name IS NOT NULL; |
All times are GMT -5. The time now is 02:18 PM. |