65

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

97

Put

SELECT ROW_COUNT();

as the last statement in your batch and parse the output

4
  • Yes, this does work, and is more elegant and easier to parse the output. Thank you it was very helpful. Jul 5, 2009 at 11:38
  • 2
    This was introduced in MySQL 5. If you're stuck using 4.x you'll have to parse the output
    – Mark Baker
    Mar 31, 2011 at 13:38
  • Use @florin.bunau's answer, it's better for seeing how many rows are updated/inserted/removed. Feb 21, 2015 at 0:09
  • 1
    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/… Oct 5, 2016 at 0:04
33

I might have answered myself the question, been looking at the parameters, and aparently using "-v -v -v" as parameters to the mysql command forces it to be more verbose and it spits out how many rows where affected.

4
  • 7
    -vvv is great if you want more details, which includes query execution time.
    – Qtax
    Feb 3, 2014 at 16:29
  • @Qtax, Is there any difference between -v and -vvv?
    – Pacerier
    Mar 30, 2015 at 5:27
  • 1
    @Pacerier, yes they are different. For each v you add you get increased verbosity and more information (like query execution times, etc).
    – Qtax
    Mar 30, 2015 at 7:53
  • 1
    @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?
    – Pacerier
    Apr 5, 2015 at 17:33
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
  • 1
    How does this help if i run an update query and want to see how many rows have changed? Jan 5, 2011 at 2:42
  • There is no "verbose dump" anywhere in any of the current answers or the question... Jan 5, 2011 at 2:48
  • 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?
    – Jason
    Jan 5, 2011 at 2:49
  • @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".
    – Jason
    Jan 5, 2011 at 2:51
  • 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 Jan 5, 2011 at 3:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.