capture deleted rows count in a variable from mysql table using bash script
0
votes
1
answer
186
views
I am trying to capture the number of deleted rows from mysql table from a bash script using
--execute
option, and it seems to be not working. I have gone through some questions where it mentions using -vv
options, it works but not as expected. I used various combinations of -vvv --silent
and --skip-column-names
with mysql command and awk and sed as well but nothing seems to work.
MySQL Client is mysql Ver 8.0.39 for Linux on aarch64 (MySQL Community Server - GPL)
[How to get number of rows deleted from mysql in shell script](https://stackoverflow.com/questions/2628310/how-to-get-number-of-rows-deleted-from-mysql-in-shell-script)
[How to get number of rows affected, while executing MySQL query from bash?](https://stackoverflow.com/questions/1083866/how-to-get-number-of-rows-affected-while-executing-mysql-query-from-bash)
[How can you output UPDATE / INSERT query results when using MySQL -e,--execute?](https://dba.stackexchange.com/questions/23527/how-can-you-output-update-insert-query-results-when-using-mysql-e-execute)
Here is the code
deleted=$(mysql --login-path=lp1 --init-command="SET SQL_LOG_BIN = 0" -vv --silent --skip-column-names db -e "delete from table1 where column1 date_format('2022-04-01 00:00:00','%Y-%m-%d 00:00:00') and date_format('2023-03-31 23:59:59','%Y-%m-%d 23:59:59') order by column1 limit 20;")
echo ${deleted}
The statement deletes certain rows but prints an output like this
-------------- delete from table1 where column1 between date_format('2022-04-01 00:00:00','%Y-%m-%d 00:00:00') and date_format('2023-03-31 23:59:59','%Y-%m-%d 23:59:59') order by column1 limit 20 -------------- Query OK, 20 rows affected -------------- 1 row in set Bye
If I add one more line to get the row_count then it gives me -1
mysql --login-path=${LP_wallet_0} -e "select row_count()"
Even if I try row_count() in the same delete query it just appends this line select row_count() -------------- 2 1 row in set Bye
I tried different combinations of --vv --silent --skip-column-names
and awk
and sed
too but I am unable to get the count.
Asked by Avinash Pawar
(216 rep)
Nov 3, 2024, 05:36 PM
Last activity: Nov 4, 2024, 10:51 AM
Last activity: Nov 4, 2024, 10:51 AM