0

How to combine these both Query into Single Query

$sql = "INSERT INTO deletedtemplate (template) SELECT template FROM template WHERE id = $id";

$sql1 = "DELETE FROM template WHERE id = $id ";

3 Answers 3

2

Simply you can combine these both query using this:

$sql = "INSERT INTO deletedtemplate (template) SELECT template FROM template WHERE id = $id; DELETE FROM template WHERE id = $id ";
Sign up to request clarification or add additional context in comments.

1 Comment

heck i coulda told you that. I didn't realize that was an acceptable answer. haha :) as its not actually one query but two. The first query ends at the semi-colon. :) I'll shoot you the upvote anyway.
1

You can not have these 2 queries together, however the same could be handled via trigger something as

delimiter //
create trigger template_del before delete on template
for each row
begin
 insert into deletedtemplate (template) 
 SELECT template FROM template WHERE id = old.id ;
end;//

delimiter ;

So if you delete something on template an insert will happen on the deletedtemplate table

2 Comments

if i put this code in function am getting error as unexpected create
You can't put this into your PHP code, you need to add this directly into mysql.
1

Try this... and maybe make the name of your field different than the name of your table... so table would still be template, but change the field currentTemplate or something like that. I'm not sure if that might be causing any issues but it makes it less confusing to read/edit the query that's for sure.

REPLACE INTO 
    `deletedtemplate`
    (`template`) 
select
    `template`
from
    `template`

where 
    `id` = $id;

8 Comments

am getting this Error : SQLSTATE[42000]: Syntax error or access violation: 1064 'as d, template as t (d.template) values (t.template) where t.id = 5' at line 1
Posted an update, my query syntax was wrong, can you try the new query I posted. @KalaiS
here record is moving to another table, but current table data not getting delete
So it's inserting just not deleting AND your getting an error?
Can you post the new error so I can see where in the query it's getting mad.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.