Home >Database >Mysql Tutorial >How to Capture the Number of Rows Affected by a MySQL Query from Bash?

How to Capture the Number of Rows Affected by a MySQL Query from Bash?

Barbara Streisand
Barbara StreisandOriginal
2024-11-02 16:06:29840browse

How to Capture the Number of Rows Affected by a MySQL Query from Bash?

Capture Number of Rows Affected During MySQL Query Execution from Bash

When executing MySQL queries through the bash command line, you can retrieve the number of rows affected by the query using a specific technique.

To obtain this information, you can incorporate the SELECT ROW_COUNT(); statement as the concluding part of your SQL command block. By doing this, you enable the query to output the number of affected rows.

To capture this output in bash, you can use backticks () or the heredoc ( << QUERY_INPUT `) syntax. However, you'll need to parse the output to isolate the row count value.

For example, to execute the UPDATE query and obtain the affected row count, you would use:

<code class="bash">variable=$(mysql -u[user] -p[pass] -e "UPDATE table_name SET column_name = 'new_value' WHERE condition; SELECT ROW_COUNT();")</code>

Once you have the output stored in the $variable, you can retrieve the row count using appropriate string manipulation techniques, such as grep or sed.

The above is the detailed content of How to Capture the Number of Rows Affected by a MySQL Query from Bash?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn