0

I am trying to read out ids from mysql and then delete the entry with id-1.

today_time=$(date -d "now" '+%d-%b-%Y %H:%M:%S')  
query_find_id="SELECT id FROM guestlist WHERE value <= '$today_time'"

array_query=$(mysql -u $USER -p$PASSWORD $TABLE_NAME -e "$query_find_id")

array_id=($(for i in $array_query; do echo $i; done))

for j in "${array_id[@]}"; k=$j-1; mysql -u $USER -p$PASSWORD $TABLE_NAME -e "DELETE FROM guestlist WHERE id = '$k'"; done

The array is empty every time. What am I doing wrong?

1
  • learn to use set -vx and examine the output from each line. You would probably see that query_find_id is empty when you get to the 3rd line. (Agreeing about answer from @tgx.) Good luck to all. Commented Sep 13, 2014 at 20:49

1 Answer 1

1

If your value column is a DATETIME, your date format in today_time should be %Y-%m-%d%H:%M:%S.

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

2 Comments

Actually my value in the list is something like '12 Sep 2014 15:00:00'
You're using WHERE value <= '$today_time' in your query. What column type is value?

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.