Results of SQL Query - mysql

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!

Related

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

How can I get the number of affected records, and just that, using mysql cmdline?

I'm running the mysql cmdline binary to execute queries. (Specifically, I'm executing
LOAD DATA LOCAL INFILE). I would like to have it print, after every statement, the number of records effected - but no other lines. I don't mind the line containing other info, e.g.
Query OK, 20 rows affected, 4 warnings
would be great. I can, in fact, get that - but only if I use the -v -v pair of switches, and then I get more info which I don't need, like an echo of the parsed command.
Can I do anything other than grep the output for the pattern of the line above?
It's maybe not the best solution, but you can try the SQL ROW_COUNT() function
E.g :
mysql ... -e "YOUR_QUERY;SELECT ROW_COUNT()" -B | tail -n 1

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)

Selecting "ALL" in a query using a list of variables

My first (simplified) script mysql_script.sh is:
#!/bin/bash
/usr/bin/mysql --password=**** --database=mydb<<EOMYSQL
SELECT SUM(usd) FROM mytable WHERE country=$VAR
INTO OUTFILE "oufile_$VAR.txt" FIELDS TERMINATED BY ';';
EOMYSQL
The first script is called from the second one:
#!/bin/bash
list=( 30 50 60 )
for L in "${list[#]}"
do
VAR=${L} ./mysql_script.sh
done
It works fine, but now I need to get a 4th query with the results for $VAR=*, or $VAR=30 or 50 or 60. As VAR is the code of every country in my DB, I need the value of SUM(usd) for "all countries".
How can I express that?
I tested '%%' and '*' without success.
Thanks,
EDITED for (I hope) better understanding
Try this:
#!/bin/bash
list=( 30 50 60 country)
for L in "${list[#]}"
do
VAR=${L} ./mysql_script.sh
done
this will result in where country=country which will always be true and thus return a sum of all rows.
Handling ORs would get trickier and I'd say at that point you'd be well served to move past bash into as scripting language like perl or python.

auto-refreshing MySQL query on Linux commandline

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