MySQL: return updated rows

后端 未结 6 618
既然无缘
既然无缘 2020-12-03 01:28

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

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

and:



        
相关标签:
6条回答
  • 2020-12-03 02:05

    There is a faster version of the return of updated rows, and more correct when dealing with highly loaded system asks for the execution of the query at the same time on the same database server

    update table_name WITH (UPDLOCK, READPAST)
    SET state = 1
    OUTPUT inserted.
    
    0 讨论(0)
  • 2020-12-03 02:05

    This is really late to the party, but I had this same problem, and the solution I found most helpful was the following:

    SET @uids := null;
    UPDATE footable
       SET foo = 'bar'
     WHERE fooid > 5
       AND ( SELECT @uids := CONCAT_WS(',', fooid, @uids) );
    SELECT @uids;
    

    from https://gist.github.com/PieterScheffers/189cad9510d304118c33135965e9cddb

    0 讨论(0)
  • 2020-12-03 02:16

    UPDATE tab SET column=value RETURNING column1,column2...

    0 讨论(0)
  • 2020-12-03 02:23

    So what you're trying to do is reset time to zero whenever you access a row -- sort of like a trigger, but MySQL cannot do triggers after SELECT.

    Probably the best way to do it with one server request from the app is to write a stored procedure that updates and then returns the row. If it's very important to have the two occur together, wrap the two statements in a transaction.

    0 讨论(0)
  • 2020-12-03 02:26

    Apparently mysql does have something that might be of use, especially if you are only updating one row.

    This example is from: http://lists.mysql.com/mysql/219882

    UPDATE mytable SET
    mycolumn = @mycolumn := mycolumn + 1
    WHERE mykey = 'dante';
    
    SELECT @mycolumn;
    

    I've never tried this though, but do let me know how you get on.

    0 讨论(0)
  • 2020-12-03 02:30

    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 ;
    
    0 讨论(0)
提交回复
热议问题