Jump to content

Saving Results of Query to Table


manhattes
Go to solution Solved by Barand,

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

Edited by manhattes
Link to comment
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"; 
Edited by manhattes
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.