70

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.

0

3 Answers 3

99

Put

SELECT ROW_COUNT();

as the last statement in your batch and parse the output

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, this does work, and is more elegant and easier to parse the output. Thank you it was very helpful.
This was introduced in MySQL 5. If you're stuck using 4.x you'll have to parse the output
Use @florin.bunau's answer, it's better for seeing how many rows are updated/inserted/removed.
I suggest you add the -s (silent) before the -e, as well as -N to remove the header row. You should get the actual numeric value only from this. See this: stackoverflow.com/questions/25539218/…
38

Looking at the documentation, apparently using -v at least 3 times forces MySQL to be more verbose and it prints how many rows where affected. Example:

mysql -vvv ...

Reference:

https://dev.mysql.com/doc/refman/8.0/en/mysql-command-options.html#option_mysql_verbose

5 Comments

-vvv is great if you want more details, which includes query execution time.
@Qtax, Is there any difference between -v and -vvv?
@Pacerier, yes they are different. For each v you add you get increased verbosity and more information (like query execution times, etc).
@Qtax, Weird, I seem to be getting the same output regardless of the number of vs. What about -vvv vs -v -v -v vs -v -vv vs -vv -v?
Interestingly, in MySQL 5.7, you just need -vv
4

Not an answer, but useful addition, you also could try the other MySQL information functions ( which include ROW_COUNT() ) to give you specific information you require. See MySQL reference here

5 Comments

How does this help if i run an update query and want to see how many rows have changed?
There is no "verbose dump" anywhere in any of the current answers or the question...
If you looked at the function reference in that link ROW_COUNT() is one of the functions and it states that it provides "The number of rows updated". So why the down vote?
@tobyodavies - my comment about verbose dump was from theBlinker's answer above "using "-v -v -v" as parameters to the mysql command forces it to be more verbose".
ok, i didn't actually see the row_count function last time i looked - i thought you linked to a page without the only function that answered the question. can't un -1 unless u edit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.