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

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.

Related

Calculating Average after MySQL Query is Run

I've run a MySQL query (this is in wordpress php):
$myQuery = $wpdb->get_results('SELECT Opponent, ROUND(AVG(Points),2)
AS Avg_Points, ROUND(AVG(Plus_Minus),2) AS Avg_Plus_Minus
FROM ' . 'afl_defense_v_position' . ' WHERE Position = "MID"
AND Rank <= 1 AND Round >= 10 GROUP BY Opponent
ORDER BY Avg_Plus_Minus DESC')
This all works fine and I can build an html table off this no problem.
What I am looking to do now though is find the standard deviation and average of the resulting Avg_Plus_Minus column and assign them to php variables so that I can use them to colour the table rows.
How do I assign these variables? (Once assigned I know how to code the colours)
I know how to do this by running another MySQL query and modifying the aforementioned code, however, I assume there is an easier way to calculate these from the array result of the original query.
Any help is appreciated.
You can install the PECL extension stats and then use the stats_standard_deviation() function to compute the standard deviation. Also look at http://php.net/manual/en/function.stats-standard-deviation.php for a pure PHP implementation (if you do not want to install the extension).

Wordnet MySQL statement doesn't complete

I'm using the Wordnet SQL database from here: http://wnsqlbuilder.sourceforge.net
It's all built fine and users with appropriate privileges have been set.
I'm trying to find synonyms of words and have tried to use the two example statements at the bottom of this page: http://wnsqlbuilder.sourceforge.net/sql-links.html
SELECT synsetid,dest.lemma,SUBSTRING(src.definition FROM 1 FOR 60) FROM wordsXsensesXsynsets AS src INNER JOIN wordsXsensesXsynsets AS dest USING(synsetid) WHERE src.lemma = 'option' AND dest.lemma <> 'option'
SELECT synsetid,lemma,SUBSTRING(definition FROM 1 FOR 60) FROM wordsXsensesXsynsets WHERE synsetid IN ( SELECT synsetid FROM wordsXsensesXsynsets WHERE lemma = 'option') AND lemma <> 'option' ORDER BY synsetid
However, they never complete. At least not in any reasonable amount of time and I have had to cancel all of the queries. All other queries seem to work find and when I break up the second SQL example, I can get the individual parts to work and complete in reasonable times (about 0.40 seconds)
When I try and run the full statement however, the MySQL command line client just hangs.
Is there a problem with this syntax? What is causing it to take so long?
EDIT:
Output of "EXPLAIN SELECT ..."
Output of "EXPLAIN EXTENDED ...; SHOW WARNINGS;"
I did more digging and looking into the various statements used and found the problem was in the IN command.
MySQL repeats the statement for every single row in the database. This is the cause of the hang, as it had to run through hundreds of thousands of records.
My remedy to this was to split the command into two separate database calls first getting the synsets, and then dynamically creating a bound SQL string to look for the words in the synsets.

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

Using MySQL in Powershell, how do I pipe the results of my script into a csv file?

In PowerShell, how do I execute my mysql script so that the results are piped into a csv file? The results of this script is just a small set of columns that I would like copied into a csv file.
I can have it go directly to the shell by doing:
mysql> source myscript.sql
And I have tried various little things like:
mysql> source myscript.sql > mysql.out
mysql> source myscript.sql > mysql.csv
in infinite variation, and I just get errors. My db connections is alright because I can do basic table queries from the command line etc... I haven't been able to find a solution on the web so far either...
Any help would be really appreciated!
You seem to not be running powershell, but the mysql command line tool (perhaps you started it in a powershell console though.)
Note also that the mysql command line tool cannot export directly to csv.
However, to redirect the output to a file just run
mysql mydb < myscript.sql >mysql.out
or e.g.
echo select * from mytable | mysql mydb >mysql.out
(and whatever arguments to mysql you need, like username, hostname)
Are you looking for SELECT INTO OUTFILE ? dev.mysql.com/doc/refman/5.1/en/select.html – Pekka 19 hours ago
Yep. Select into outfile worked! But to make sure you get column names you also need to do something like:
select *
from
(
select
a,
b,
c
)
Union ALL
(Select *
from actual)