Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I do a mysql Select, Update and Delete in one query?

Can I say that one of many ways to optimize mysql is to reduce the number of queries?

If that so, can I do this:

- Select "data" => $A from table X
- Update $A from table Y
- Delete $A from table X

in one query?

like image 828
webdev_007 Avatar asked Dec 03 '10 09:12

webdev_007


1 Answers

You can't reduce the number of queries - they all do different things - but you could reduce the number of round trips to the database and the number of parses by wrapping it all as a PLSQL function.

However you can't select the data after you've deleted it.....but consider:

CREATE PROCEDURE s_u_d(a)
BEGIN

UPDATE tab_x SET tab_x.avalue=1 WHERE tab_x.another=a;

DELETE FROM tab_y WHERE tab_y.avalue=a;

SELECT * 
FROM tab_x
WHERE tab_x.another=a;

END;

NB - you can also run multiple selects in the same procedure and handle multiple, different shaped result sets, e.g. see this page

like image 137
symcbean Avatar answered Sep 30 '22 03:09

symcbean