MySQL 从bash执行MySQL查询时如何获取受影响的行数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1083866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:37:36  来源:igfitidea点击:

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

mysqlbash

提问by florin.bunau

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

我知道如何从 bash 执行 MySQL 查询/命令:

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.

它确实执行了命令,但不返回受影响的行数。

回答by Ken Keenan

Put

SELECT ROW_COUNT();

as the last statement in your batch and parse the output

作为批处理中的最后一条语句并解析输出

回答by florin.bunau

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.

我可能已经回答了自己这个问题,一直在查看参数,并且显然使用“-v -v -v”作为 mysql 命令的参数会强制它变得更加冗长,并且它会吐出受影响的行数。

回答by Jason

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

不是答案,而是有用的补充,您还可以尝试其他 MySQL 信息函数(包括 ROW_COUNT() )来为您提供所需的特定信息。 请参阅此处的 MySQL 参考