Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>you can do it in sql. should be something like this:</p> <pre><code>INSERT INTO tbl_dest (first_name, last_name) VALUES (SELECT LEFT(ts.name, LOCATE(' ',ts.name)) AS first_name, SUBSTRING(ts.name, LOCATE(' ',ts.name)+2) AS last_name FROM tbl_source AS ts) </code></pre> <p>as @Marc B stated, it will only work if the data is consistent (ie each row's name field contains a double space and the first double space separates the name and password. you can ignore other rows (without the double space) by simply adding </p> <pre><code>INSERT ... (SELECT ... FROM tbl_source AS ts WHERE LOCATE(' ', ts.name) &gt; 0) </code></pre> <p>if the field starts with a double space (ie first name is empty) it will also be ignored</p> <p><strong>EDIT:</strong></p> <p>since you said that each row can contain multiple pairs of first/last name. here's a code example:</p> <pre><code>$query = "SELECT `name` FROM `tbl_source`"; $res = mysql_query($query, $link); $names = array(); while ($row = mysql_fetch_assoc($res)) { $nmlist = explode(' ',$row['name']); for($i=0, $n=count($nmlist); $i&lt;$n-1; $i+=2){ $names[] = "('" . mysql_real_escape_string(stripslashes($nmlist[$i])) ."'," ."'" . mysql_real_escape_string(stripslashes($nmlist[$i+1])) ."')"; } } $insert_query = "INSERT INTO `tbl_dest` (`first_name`,`last_name`) VALUES " .implode(',', $names); mysql_query($insert_query, $link); </code></pre> <p>in this example, double spaces are between first name and last name, and between each pair</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload