Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get number of rows affected, while executing MySQL query from bash?

Tags:

bash

mysql

I know how one can execute MySQL queries / commands from bash:

mysql -u[user] -p[pass] -e "[mysql commands]" 

or

mysql -u[user] -p[pass] `<<`QUERY_INPUT  [mysql commands]  QUERY_INPUT 

How can I capture how many rows were affected by the query?
I tried doing:

variable='`mysql -u[user] -p[pass] -e "[mysql commands]"`' 

It does execute the command but it does not return the number of affected rows.

like image 645
florin.bunau Avatar asked Jul 05 '09 11:07

florin.bunau


People also ask

How can I count the number of rows 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() .

Which command is used to retrieve number of rows in MySQL?

Using MySQLi If you want to fetch all the rows, but still know the number of rows then you can use num_rows or count() .


1 Answers

Put

SELECT ROW_COUNT(); 

as the last statement in your batch and parse the output

like image 100
Ken Keenan Avatar answered Oct 02 '22 03:10

Ken Keenan