When I run this command I get a full table output....
mysql --user=root --password="mypassword" -e "SELECT btce_last_price FROM api.btc WHERE id=1"
+-----------------+
| btce_last_price |
+-----------------+
| 723 |
+-----------------+
I would like the output to be just "723".
Use the silent mode -s to produce less output. You can also add raw -r and --skip-column-names
The -N switch removes column names and you can use awk to strip the tabular formatting (whitespace, pipe & dash symbols):
SQL='SELECT btce_last_price FROM api.btc WHERE id=1'
mysql -u root -p "mypassword" -N -e "$SQL"|awk '{print $1}'
Try
mysql --user=root --password="mypassword" -e "SELECT btce_last_price FROM api.btc WHERE id=1" | tr -dc '[0-9]'
That'll get you your bitcoin prices by pulling out only the numbers from that output.
If you aren't just looking for numbers, you can use
mysql --user=root --password="mypassword" --skip-column-names -e "SELECT btce_last_price FROM api.btc WHERE id=1" | tr -d '[\+\-\| ]'
As long as there aren't and +, -, | or spaces in your value... otherwise you have to get trickier.
Related
I use a query command from bash which returns a number. However, it is printed as a table.
$ mysql -u muser -p$PASS mm -e "SELECT.....;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------------------------+
| COUNT(DISTINCT ula.userid) |
+----------------------------+
| 29 |
+----------------------------+
I just want to get 29 and append that to a file with >> file.txt. How can I do that in mysql?
Use skip-column-names and batch-mode with -N and -B respectively:
mysql -u muser -p$PASS mm -NBe "SELECT.....;" >> file.txt
I am trying to create a bash shell script that runs an sql query and later on create a cronjob that runs it at an specific time.
I created my bash script see below
mysql -u $host -D $dbname -u $user -p$password -e $mySqlQuery
I have wrap -u -D -p -e all in variables. I have also change it to and executable file. When i run it. it gives out an output stating. Command not found. can anyone tell the mistake i made?
Below is the bash script
host="host"
user="user"
dbname="database"
password="password"
mySqlQuery = "SELECT *
FROM invoice i
JOIN item it
ON it.invoice_id = i.id
JOIN user u
ON i.user_id = u.id
JOIN gateway_response gr
ON gr.invoice_id = i.id
WHERE i.created_at >= '2019-03-01 00:00:00' and
i.created_at <= '2019-03-17 23:59:59' and i.status=9"
mysql -u $host -D $dbname -u $user -p$password -e $mySqlQuery
Below is the error i am receiving when i run it.
/home/chris2kus/givingDetectRun.sh: line 8: mySqlQuery: command not found /home/chris2kus/givingDetectRun.sh: line 20: mysql: command not found –
There must be no spaces around the = and the variable name mySqlQuery.
Also, I suggest you wrap your variables around double quotes, i.e., use "$host" instead of just $host.
You can write a file like this and chmod 755 filename.sh :
#!/bin/bash
host="localhost"
dbname="test"
user="root"
password="xxxxxxxxxx"
mySqlQuery="select *
from col;"
mysql -u $host -D $dbname -u $user -p$password -e "$mySqlQuery"
Sample
$chmod 755 testmysql.sh
$
$ ./testmysql.sh
+----+------+------+------+
| id | Col1 | Col2 | Col3 |
+----+------+------+------+
| 1 | 1 | 2 | 3 |
| 2 | 2 | 3 | 4 |
| 3 | 3 | 4 | 5 |
+----+------+------+------+
$
For starters, make sure your line 5 looks like
mySqlQuery="SELECT..."
(notice no spaces on either side of the assignment operator)
For seconds, try to re-format your entire MySQL query to fit into a single line.
(perhaps Heidi since it's a query, since that will keep you at least syntax-wise in the clear of errors)
For thirds, once you confirm that the bash runs as intended, add \n to tell the bash that you're continuing the command in the next row
Prototyping before optimization. Get it running before you get it flying.
I am trying to get count of column user_Id using count(user_Id) from mysql as follows:
count=$(mysql -uroot -proot csv_imports -e "select count(user_Id) from test_data where user_Id=\"12345\";")
I am not getting what is wrong with it. I want it's numeric result. What could help me?
Using options -B -N in command mysql
--batch, -B - Print results using tab as the column separator, with each row on a new line. With this option, mysql does not use the history file.
--skip-column-names, -N - Do not write column names in results.
count=$(mysql -uroot -proot csv_imports -B -N -e "select count(user_Id) from test_data where user_Id=\"12345\";»)
without options -B -N result is:
+----------------+
| count(user_id) |
+----------------+
| 4 |
+----------------+
with option -B result is:
count(user_id)
4
with option -B -N result is:
4
How can i store mysql databases in linux using shell scripting
script:
mysql -uusername -hhostname -ppassword -e "show databases"
I think you want this: http://lists.mysql.com/mysql/96132
mysql> use mysql
Database changed
mysql> tee /tmp/mysqltee
Logging to file '/tmp/mysqltee'
mysql> show tables;
+-----------------+
| Tables_in_mysql |
+-----------------+
| columns_priv |
| db |
| host |
| tables_priv |
| user |
+-----------------+
5 rows in set (0.02 sec)
mysql> notee
Outfile disabled.
mysql>
If the file exist, the output will be appended to the existing file (/tmp/mysqltee).
As you can see, the output is also displayed on the screen. This may not be
what you want, especially if the output is big... You could use
mysql -e "select table_name from user_tables" database > output.txt
or
mysql database < script.sql > output.txt
from the os command line. (You may also need to use -u, -p and/or -h, use
the same as when you do a 'normal' start of the mysql client.)
it may help you
#!/bin/bash
results=($(mysql --user root -pwelcome -Bse "show databases;"))
The following code will retrieve all database names into a variable called dbnames. After that, it iterates will just echo a string with each name individually
#!/bin/bash
dbnames=`mysql --user=user --password=password -se "show databases;"`
for x in $dbnames;
do
echo "There is a database called $x"
done;
I have a problem with the MySQL output formatting while executing the commands from a bash script.
If I execute a command on the command line then, I am able to get the output in formatted as expected.
$ mysql -u dbclient -pxxxx GEKONYLOGDB -e "select now(),max(time_stamp) from metrics"
+---------------------+---------------------+
| now() | max(time_stamp) |
+---------------------+---------------------+
| 2012-12-09 14:25:38 | 2012-12-09 14:25:20 |
+---------------------+---------------------+
But where as if I keep the same command in a script and execute I am not getting the formatted output.
$ cat test
#!/bin/bash
mysql -u dbclient -pxxxx GEKONYLOGDB -e "select now(),max(time_stamp) from metrics"
$ ./test
now() max(time_stamp)
2012-12-09 14:27:52 2012-12-09 14:27:47
So all I need the same output from script.
Thanks.
Pass the -t or --table option to force table output.
mysql --table -u dbclient -pxxxx GEKONYLOGDB -e "select now(),max(time_stamp) from metrics"
From mysql --help:
-t, --table Output in table format.
If you want to have --table option enabled by default when calling mysql program, please check this SO answer How to store MySQL results to a file in table format.