laado Posted May 26, 2009 Share Posted May 26, 2009 I have a question about SQL statement. Suppose I have two tables name “News” and “history”. I want to delete news from news table and insert this news (deleted news) into the history table. I want to know the SQL query what will be the SQL Statement of above mention problem. Please reply as soon as possible. Thanx Link to comment Share on other sites More sharing options...
justsomeguy Posted May 26, 2009 Share Posted May 26, 2009 It's not one statement, it's more than one. You can use SELECT INTO to copy rows (assuming both tables have the same structure and you aren't violating any constraints) or INSERT..SELECT, and after you've inserted then you use a DELETE query to delete from the original table. Link to comment Share on other sites More sharing options...
laado Posted May 27, 2009 Author Share Posted May 27, 2009 i write following code but it cannt work why?<title>Delete News</title><body=background=new2.jpg><?phpglobal $title;global $text1;global $text2;global $dtime;include("config.php"); $newsid = $_GET['newsid'];$result = mysql_query("select from news (title, dtime, text1, text2) VALUES ('$title',$dtime,'$text1','$text2')",$connect);$result = mysql_query("insert INTO history (title, dtime, text1, text2) VALUES ('$title',$dtime,'$text1','$text2')",$connect); $newsid = $_GET['newsid']; $result = mysql_query("DELETE FROM news WHERE newsid='$newsid' ",$connect); ?>plz tell me whts the problem? It's not one statement, it's more than one. You can use SELECT INTO to copy rows (assuming both tables have the same structure and you aren't violating any constraints) or INSERT..SELECT, and after you've inserted then you use a DELETE query to delete from the original table. Link to comment Share on other sites More sharing options...
justsomeguy Posted May 27, 2009 Share Posted May 27, 2009 That is not valid syntax for a SELECT statement. Review this page:http://dev.mysql.com/doc/refman/5.0/en/select.htmlIf you're trying to get the values from that table and store the data in the global variables, that's not the correct way to do it. The steps are to 1) use mysql_query to get the result set, 2) loop through each row using a while loop with mysql_fetch_assoc to get each row, 3) assign the data from each row to your global variables. You would need to move the insert statement into the loop so that it runs the insert for each row.Or, like I said, you can use a single INSERT..SELECT statement to do all of that.http://dev.mysql.com/doc/refman/5.0/en/insert-select.html Link to comment Share on other sites More sharing options...
laado Posted May 28, 2009 Author Share Posted May 28, 2009 what will be the sql statement if i want to choose yesterday date. That is not valid syntax for a SELECT statement. Review this page:http://dev.mysql.com/doc/refman/5.0/en/select.htmlIf you're trying to get the values from that table and store the data in the global variables, that's not the correct way to do it. The steps are to 1) use mysql_query to get the result set, 2) loop through each row using a while loop with mysql_fetch_assoc to get each row, 3) assign the data from each row to your global variables. You would need to move the insert statement into the loop so that it runs the insert for each row.Or, like I said, you can use a single INSERT..SELECT statement to do all of that.http://dev.mysql.com/doc/refman/5.0/en/insert-select.html Link to comment Share on other sites More sharing options...
justsomeguy Posted May 28, 2009 Share Posted May 28, 2009 Check the examples here:http://dev.mysql.com/doc/refman/5.1/en/dat...nction_date-addSELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY)SELECT CURDATE() - INTERVAL 1 DAY Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.