Search code examples
mysqlreturn

MySQL: return updated rows


I am trying to combine these two queries in twisted python:

SELECT * FROM table WHERE group_id = 1013 and time > 100;

and:

UPDATE table SET time = 0 WHERE group_id = 1013 and time > 100

into a single query. Is it possible to do so?

I tried putting the SELECT in a sub query, but I don't think the whole query returns me what I want.

Is there a way to do this? (even better, without a sub query) Or do I just have to stick with two queries?

Thank You,

Quan


Solution

  • You can't combine these queries directly. But you can write a stored procedure that executes both queries. example:

    delimiter |
    create procedure upd_select(IN group INT, IN time INT)
    begin
        UPDATE table SET time = 0 WHERE group_id = @group and time > @time;
        SELECT * FROM table WHERE group_id = @group and time > @time;
    end;
    |
    delimiter ;