auto-refreshing MySQL query on Linux commandline - mysql

I would like to see a auto refreshing MySQL query result in my linux console.
something like this:
while [ 1 ]
do
clear
//some special way of executing 'select id from updates order by id desc limit 1'
sleep 1
done
Thanks

Something like the following should work:
watch -n 1 "mysql -e 'select id from updates order by id desc limit 1' your_db"

I know this is an old question but I stumbled upon it while trying to figure out the same thing and the answer is not quite right. MySQL and Watch don't seem to play nice together unless you wrap the entire mysql command in double quotes like this...
watch -n 1 "mysql -e 'select id from updates order by your_db.id desc limit 1'"
Without the double quotes, I kept getting the help info rather than the results from my select. I had to have the user name and password parms to make my query work which isn't recommended. Probably isn

Related

Detect and delete line break directly out of mysql query

im trying to detect and delete a line break out of a subject (called m.subject) mail information retrieved via CONCAT out of a mysql database.
That said, the linebreak may or may not occur in the subject and therefore must be detected.
My query looks like this:
mysql --default-character-set=utf8 -h $DB_HOST -D $TARGET -u $DB_USER -p$DB_PW -N -r -e "SELECT CONCAT(m.one,';',m.two,';',m.three,';',m.subject,';',m.four';',m.five,';',(SELECT CONCAT(special_one) FROM special_$SQL_TABLE WHERE msg_id = m.six ORDER BY time DESC LIMIT 1)) FROM mails_$SQL_TABLE m WHERE m.rtime BETWEEN $START AND $END AND m.seven = 1 AND m.eight IN (2);"
I tried to delete it afterwards, but getting in performance trouble due to several while operations on all lines already. Is there an easy way to detect and cut it directly via the CONCAT buildup? It is crucial to retrieve only one line after extraction for me.
Updating/changing the database is not an option for me, as I only want to read the current state.

Results of SQL Query

I am running a SQL query to know the number of times that the same value is on a column.
Query:
SELECT COUNT(CustomerID) AS OrdersFromCustomerID7
FROM Orders
WHERE CustomerID=7;
Query Result:
OrdersFromCustomerID7
---------------------
4
Since I want to put the "4" value to a Linux Environment Variable, I would need to remove the OrdersFromCustomerID7 text from the result. Does anyone know how to accomplish this?
Note: I have already tried removing the AS OrdersFromCustomerID7 from the query and that makes the query result to come like below:
COUNT(CustomerID)
-----------------
4
I need the query result to be returned as a single number since this is afterwards put into an environment variable for future analysis by another script.
Any ideas?
Thank you!
I was finally able to get it to work using the following modifiers in the query:
mysql "DATABASE" -N -s -r "SQL STATEMENT"
-N removes headers
-s removes separator chars
-r raw output
Thank you #Ambrish for your time!

Cron Bash - select data from two MySQL tables & export to CSV

I am really bad at creating MySQL queries and need some help. I need to create a bash file to be triggered by a cron job once a week - that queries two tables, grabbing data where the user IDs match in both tables, and adding the select data to a CSV export file. I would like the CSV to be comma separated. Right now the best I can get it tab separated.
My issue in getting this query to run is my syntax (which I know is wrong as I have simply stolen snippets from various articles online). I did get each DB query to work separately (grabbing from one table with one query and another table with another query). Now I need to combine them to grab only the data I need.
Here's my current (non working) query:
#!/bin/bash
mysql -u USERNAME --password=PASSWORD --database=xxxx_DBNAME --execute='SELECT `xxxx_videotraining_user.user_id`, `xxxx_videotraining_user.training_title`, `xxxx_videotraining_user.status`, `xxxx_users.id`, `xxxx_users.name`, `xxxx_users.user_employer`, `xxxx_users.user_ss_number` WHERE `xxxx_videotraining_user.user_id` = `xxxx_users.id` AND `xxxx_videotraining_user.status` = "Completed" AND `xxxx_users.user_ss_number` > "1" ORDER BY `xxxx_videotraining_user.user_id` LIMIT 0, 10000 AND ' -C > /home/xxxx/subs/vtc/DB_EXPORTS/xxxx_videotraining_completed.csv
I think you can see what I am trying to accomplish here - any help would be greatly appreciated!
It also looks like you're missing your FROM clause, have an trailing AND clause (as noted in other answers), and are quoting things incorrectly. This looks to be your original query:
SELECT `xxxx_videotraining_user.user_id`,
`xxxx_videotraining_user.training_title`,
`xxxx_videotraining_user.status`,
`xxxx_users.id`,
`xxxx_users.name`,
`xxxx_users.user_employer`,
`xxxx_users.user_ss_number`
WHERE `xxxx_videotraining_user.user_id` = `xxxx_users.id` AND
`xxxx_videotraining_user.status` = "Completed" AND
`xxxx_users.user_ss_number` > "1"
ORDER BY `xxxx_videotraining_user.user_id`
LIMIT 0, 10000 AND
I think you want to add the FROM clause, quote the table and field separately, and remove the trailing AND, to get something like:
SELECT `xxxx_videotraining_user`.`user_id`,
`xxxx_videotraining_user`.`training_title`,
`xxxx_videotraining_user`.`status`,
`xxxx_users`.`id`,
`xxxx_users`.`name`,
`xxxx_users`.`user_employer`,
`xxxx_users`.`user_ss_number`
FROM `xxxx_users`,
`xxxx_videotraining_user`
WHERE `xxxx_videotraining_user`.`user_id` = `xxxx_users`.`id` AND
`xxxx_videotraining_user`.`status` = "Completed" AND
`xxxx_users`.`user_ss_number` > "1"
ORDER BY `xxxx_videotraining_user`.`user_id`
LIMIT 0, 10000
There are other things that could be done to shorten the size of the query and make it a bit cleaner, but that should get it functional.
One thing I know that helps me when dealing with long queries is to format them like this, with the main clauses separated out so you can see the different sections of the query.
Let me know if that helps.
I think AND shouldn't be here:
LIMIT 0, 10000 AND

execute MySQL query from the terminal without printing results

I am performing some MySQL queries that have very large result sets. I would like to see how long they take, but I don't want all the output to be printed on my terminal because it takes up a lot of space and time. I can do this by writing a script that performs and times the query, but I was wondering if there was a way to do this directly through MySQL on the terminal. Thanks.
Change the pager in mysql like indicated here: http://www.mysqlperformanceblog.com/2013/01/21/fun-with-the-mysql-pager-command/
mysql> pager cat > /dev/null will discard the output, and mysql> pager will put it back.
Wrap your query in set #foo = (select count(*) from ( ..... ) foo)
Just run mysql console utility, then enter source file_name (where file_name contains sql commands)

How can i set a crontab to execute a mysql query and log the output?

Well, title self describes it..
I need to run a sql function to clean some duplicated posts, i need to do it several times a day so i need to use cron...
I set a new crontab job, like this:
00 16,18,19,20,21 * * * mysql -h MY-DB-HOST.COM -u MY-DB-USERNAME -pMY-DB-PASSWORD -e "delete from hotaru_posts where post_id in ( select post_id from ( select post_id from hotaru_posts a group by post_title having count(post_title) > 1 ) b )" >> /tmp/cron_job.log
but nothing seems to be logged, so i supposed its not working.
Theres no problem with the sql sentence, thats not the issue here.
Anything wrong with my cron rule?
well, since the mysql was not working properly directly inside crontab (thought that i think that was a path issue like Alex Howansky said), i created a php file dealing this query and called the php in crontab, much easier, and give me the option to use conditions.
the cron job:
00 8,14,18,19,20,21,23 * * * /usr/local/bin/php /home/aikaforum/cata/public_html/cron_dup.php >> /cata/tmp/cron_dup.log
the php:
<?php
$username="xxxxxxx";
$password="xxxxxx";
$dbname="xxxxxx";
$dbhost="xxxxx.xxxxx.com";
$query="delete from hotaru_posts where post_id in ( select post_id from ( select post_id from hotaru_posts a group by post_title having count(post_title) > 1 ) b )";
mysql_connect($dbhost,$username,$password);
#mysql_select_db($dbname) or die(strftime('%c')." Unable to select database");
mysql_query($query);
mysql_close();
echo strftime('%c')." ok!";
?>
Thanks for all the help.
execute the following in your cron configurations
echo "your_SQL_statement" | mysql --skip-column-names -udbuser -pdbpassword yourdb >> yourlog.log
I suspect that your script is working but isn't actually returning any output. Here's my local test:
mysql -u username dbname -e "delete from posts" > foo
cat foo
(empty file)
Just to be clear, foo is an empty file. posts was not an empty table.
So, just to be more precise, I think that's the expected behavior of MySQL here, although I can't confirm this in their docs. If you want/need output here, you'll probably need to write a script to check your table before/after deleting.
You need to use the full path to scripts executed by a cronjob. For instance if the mysql binary's location is /usr/local/mysql/bin/mysql you'd use that in your cronjob.
00 16,18,19,20,21 * * * /usr/local/mysql/bin/mysql -h MY-DB-HOST.COM -u .....