Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: return updated rows

Tags:

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

like image 668
Squall Leohart Avatar asked Jul 13 '12 19:07

Squall Leohart


People also ask

What is return value of update query in MySQL?

"The update() method is applied instantly and returns the number of rows affected by the query." The actual return value depends on the database backend. MySQL, for example, will always return 1 if the query is successful, regardless of the number of affected rows.

How check row is affected in MySQL?

mysql_affected_rows() may be called immediately after executing a statement with mysql_real_query() or mysql_query() . It returns the number of rows changed, deleted, or inserted by the last statement if it was an UPDATE , DELETE , or INSERT . For SELECT statements, mysql_affected_rows() works like mysql_num_rows() .

What is the return of update query?

Executed update queries always return the number of rows matched by the query, including rows that didn't have to be updated because their values wouldn't have changed.

What is the update query in MySQL?

The MySQL UPDATE query is used to update existing records in a table in a MySQL database. It can be used to update one or more field at the same time. It can be used to specify any condition using the WHERE clause.


1 Answers

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.

like image 108
AW101 Avatar answered Sep 21 '22 08:09

AW101