Delete a record after making a query?

0

Is there a way, after a query, to delete the result returned from a database?

I have a message table (from, to, message) each time I consult should I delete the read message (s) read.

    
asked by anonymous 02.12.2014 / 23:56

1 answer

0

You can create a Stored Procedure in your database. Here's an example:

DELIMITER $$

USE 'database'$$

DROP PROCEDURE IF EXISTS 'consultaMensagem'$$

CREATE PROCEDURE 'consultaMensagem'(IN idMensagem INT)
BEGIN
    SELECT * FROM mensagem WHERE id = idMensagem;   
    DELETE FROM mensagem WHERE id = idMensagem;
END$$
DELIMITER ;

Being:

database - > your database queryMessage - > name of your procedure idMessage - > input parameter for query and deletion

Between BEGIN and END you develop your procedure, in case this executes a query in the database filtering all the records that have the id = idMensagem (parameter passed to the procedure). Then, with the same filter, it executes a DELETE , thus eliminating the queried message.

To execute procedure , do this:

Call consultaMensagem(1);

So the message with id = 1 will be returned and deleted soon.

    
03.12.2014 / 10:25