Is it possible in MySQL to update a single field and select multiple different fields all where a single condition is true, in a single query? I'm not exactly sure of what the proper SQL would be, but I want to merge these two example queries, if it is possible, of course.
An update query:
UPDATE `users` SET `activation`='$activationCode' WHERE `email`='$anEmail'
and a select query:
SELECT `password`,`salt`,`fname`,`email` FROM `users` WHERE `email`='$anEmail'
See the duplicate condition here? Surely it's unnecessary to query a MySQL database twice with duplicate conditions, when I could do it all at once under a single condition?
It's worth noting that $anEmail
and $activationCode
would be previously defined variables in PHP (with the former also being exactly the same through both queries). Cheers.
You have to write a stored procedure .
delimiter ;;
CREATE PROCEDURE UpdateAndSelect (mEmail Varchar(50), mactivationCode Varchar(20))
BEGIN
START TRANSACTION;
UPDATE `users` SET `activation`=mactivationCode WHERE `email`=mEmail;
SELECT `password`,`salt`,`fname`,`email` FROM `users` WHERE `email`=mEmail;
COMMIT;
END;;
delimiter ;
Call the procedure with:
CALL UpdateAndSelect(:email, :activation)
If you just want to skip the repeated where clause then check this post (first answer)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.