Jump to content

Saving Results of Query to Table


manhattes

Recommended Posts

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>";

Link to comment
https://forums.phpfreaks.com/topic/297559-saving-results-of-query-to-table/
Share on other sites

 

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"; 

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

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

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. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.