manhattes Posted July 30, 2015 Share Posted July 30, 2015 (edited) I would like to save the results of this query to a table. What should I add under the WHILE statement? What would the format of the INSERT INTO statement? $query = "SELECT s.SName, s.Symbol, c.`Interventions`, c.`Study Results`, c.`Completion Date`, c.`First Received`, c.`Primary Completion Date` FROM Stocks s LEFT JOIN Calendar as c ON c.SName LIKE CONCAT('%', s.SName, '%' )LIMIT 20"; $sql51="INSERT INTO CombinedList $query)"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_assoc($result)){ $result51 = mysql_query($sql51); echo "Moved " . $row[SName] . " from searchable database into revised search table."; echo "<br>"; Edited July 30, 2015 by manhattes Quote Link to comment Share on other sites More sharing options...
Barand Posted July 30, 2015 Share Posted July 30, 2015 Specify the columns to receive the data INSERT INTO combinedList (col, names, go, here,) SELECT ..... Quote Link to comment Share on other sites More sharing options...
manhattes Posted July 30, 2015 Author Share Posted July 30, 2015 (edited) Specify the columns to receive the data INSERT INTO combinedList (col, names, go, here,) SELECT ..... Thank you Barand, I was going in that direction. Do you have to list all of the columns that are in the table? Or only the ones I want filled in? Thank you for your help! $query = "INSERT INTO CombinedList (SName2, Symbol, Interventions, Study Results, Completion Date, First Received, Primary Completion Date) SELECT s.SName, s.Symbol, c.`Interventions`, c.`Study Results`, c.`Completion Date`, c.`First Received`, c.`Primary Completion Date` FROM Stocks s LEFT JOIN Calendar as c ON c.SName LIKE CONCAT('%', s.SName, '%' )LIMIT 20"; Edited July 30, 2015 by manhattes Quote Link to comment Share on other sites More sharing options...
Barand Posted July 30, 2015 Share Posted July 30, 2015 Just those that you want filled in Quote Link to comment Share on other sites More sharing options...
manhattes Posted July 30, 2015 Author Share Posted July 30, 2015 Just those that you want filled in $query = "INSERT INTO CombinedList (Sname2, Symbol, First Received, Completion Date, Results First Received, Primary Completion Date) SELECT s.SName, s.Symbol, c.`Interventions`, c.`Study Results`, c.`Completion Date`, c.`Results First Received`, c.`Primary Completion Date` FROM Stocks s LEFT JOIN Calendar as c ON c.SName LIKE CONCAT('%', s.SName, '%' )LIMIT 20"; $result = mysql_query($query) or die(mysql_error()); I am getting the error: 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 'Received, Completion Date, Results First Received, Primary Completion Date) SEL' at line 1 Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted July 30, 2015 Solution Share Posted July 30, 2015 Don't have column names containing spaces, use underscores. (If you really must, surround the name with backticks (note: not single quotes) eg `Completion Date`) Quote Link to comment Share on other sites More sharing options...
manhattes Posted July 30, 2015 Author Share Posted July 30, 2015 Don't have column names containing spaces, use underscores. (If you really must, surround the name with backticks (note: not single quotes) eg `Completion Date`) Awesome that was it. Now it Says: Duplicate entry '' for key 'PRIMARY' When I look in the DB it seems to only do the first row of data and then stop Quote Link to comment Share on other sites More sharing options...
manhattes Posted July 30, 2015 Author Share Posted July 30, 2015 Awesome that was it. Now it Says: Duplicate entry '' for key 'PRIMARY' When I look in the DB it seems to only do the first row of data and then stop I fixed it by REMOVING the Primary Index. I will use a different column to keep track.. Thank you again Barand. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.