Is it possible to display query results like below within mysql shell?
mysql> select code, created_at from my_records;
code created_at
1213307927 2013-04-26 09:52:10
8400000000 2013-04-29 23:38:48
8311000001 2013-04-29 23:38:48
3 rows in set (0.00 sec)
instead of
mysql> select code, created_at from my_records;
+------------+---------------------+
| code | created_at |
+------------+---------------------+
| 1213307927 | 2013-04-26 09:52:10 |
| 8400000000 | 2013-04-29 23:38:48 |
| 8311000001 | 2013-04-29 23:38:48 |
+------------+---------------------+
3 rows in set (0.00 sec)
The reason I'm asking because I have some tedious task that I need to copy the output and paste it on other tool.
--raw, -r
For tabular output, the “boxing” around columns enables one column value to be distinguished from another. For nontabular output (such as is produced in batch mode or when the --batch or --silent option is given), special characters are escaped in the output so they can be identified easily. Newline, tab, NUL, and backslash are written as \n, \t, \0, and \\. The --raw option disables this character escaping.
The following example demonstrates tabular versus nontabular output and the use of raw mode to disable escaping:
% mysql
mysql> SELECT CHAR(92);
+----------+
| CHAR(92) |
+----------+
| \ |
+----------+
% mysql --silent
mysql> SELECT CHAR(92);
CHAR(92)
\\
% mysql --silent --raw
mysql> SELECT CHAR(92);
CHAR(92)
\
From MySQL Docs
Not exactly what you need, but it might be useful. Add \G at the end of the query
select code, created_at from my_records\G;
Query result will look like this:
*************************** 1. row ***************************
code: 1213307927
created_at: 2013-04-26 09:52:10
*************************** 2. row ***************************
code: 8400000000
created_at: 2013-04-29 23:38:48
One-liner
mysql -u YOURUSER -p --password=YOURPASSWORD -s -r -e "show databases;"
mysql -u root -p --password=abc12345 -s -r -e "show databases;"
You need to pass the -s parameter mysql -s.
mysql --skip-column-names --silent --raw --execute "select * from somewhere;"
To make these the default options for your user, add the following to ~/.my.cnf
skip_column_names
silent
raw
I solved this but using concat_ws to join the results together and then add a space (first argument)
select concat_ws (' ',ipNetFull,ipUsage,broadcast,gateway) from ipNets;
If you want to get MySQL client output without the surrounding table, you can run your query from the linux command line instead of through the mysql client:
$ echo "SELECT CONCAT_WS(' ','DROP TABLE',TABLE_NAME,';') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE '%';" | mysql dbname
bonus: you can pipe the output through sort and stuff, or even back into mysql:
$ echo "SELECT CONCAT_WS(' ','DROP TABLE',CONCAT_WS('.',TABLE_SCHEMA,TABLE_NAME),';') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'i\'m\_not\_that\_mean';" | mysql | mysql;
Quick Workaround:
Using the more recent MySQL clients (not sure which versions), you can use the PAGER directive to manipulate the results. Note that you may have to change the sed expression to avoid clobbering your data.
> PAGER sed 's/|//g'
PAGER set to 'sed 's/|//g''
barracuda#db-ess-rw.us-east-2.ess.aws.vt2.com [ess]
> SELECT 'row1_col1' AS column1, 'row1_col2' AS column2
UNION ALL SELECT 'row2_col1', 'row2_col2';
+---------+---------+
column1 column2
+---------+---------+
row1_col1 row1_col2
row2_col1 row2_col2
+---------+---------+
2 rows in set (0.00 sec)
Beefore you do this, you may want to get the current value of your PAGER:
> PAGER
PAGER set to 'less'
To reset to using 'less' as the PAGER
> PAGER less
PAGER set to 'less'
More fun with PAGER:
https://www.percona.com/blog/2013/01/21/fun-with-the-mysql-pager-command/
So confused by the question and answers. ran sql and result kept displaying without lines or non tabular format. Later realized there is an option when you right click on the query space, navigate to "results to" > "results to grid". This Should set it back to tabular format.
There is also an option for "results to text" which was the format I was experiencing.
:)!
Related
I am simply trying to understand why these 2 commands have different outputs on my screen:
$> mysql -ularavel -ppassword -e 'select id from queue.jobs;'
+-------+
| id |
+-------+
| 20945 |
| 20946 |
+-------+
$> watch "mysql -ularavel -ppassword -e 'select id from queue.jobs;'"
Every 2.0s: mysql -ularavel -ppassword -e 'select id from queue.jobs;'
id
20945
20946
Notice that the watch command does not draw the table borders. I simplified this example, but for multiple columns the table is distorted and difficult to read.
So, why? Is there a difference between the input/output of the watch command that is different from what's directly in the terminal?
Tried on OSX with iTerm2 and the default Terminal app
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;
Just wondering if there's a command line argument in mysql to remove the lines surrounding a mysql query result.
For one of my recent queries, I got this back:
+----------+
| count(*) |
+----------+
| 1016442 |
+----------+
Since I'd like to use this result in my shell script to do something else with it, I'd like it to just return the value, and not the +-----+ and | characters surrounding it. Is it possible to do this or do I have to find a way to parse around it? Thanks in advance for your help.
EDIT: I was hoping there was a mysql command line option to easily return just the result. If not, then I'll use sed, awk, grep like someone mentioned in the comments =)
use -s for what you want.
$ mysql -s
mysql> SELECT NOW();
NOW()
2014-04-25 10:11:57
-s means silent
$ mysql --help
...
-s, --silent Be more silent. Print results with a tab as separator,
Also you can skip column header with -N
$ mysql -s -N
mysql> SELECT NOW();
2014-04-25 10:14:49
Is there a way to disable escape characters in a MySQL query? For example, for the following table:
mysql> select * from test1;
+------------------------+-------+
| name | value |
+------------------------+-------+
| C:\\media\data\temp\ | 1 |
| C:\\media\data\temp | 2 |
| /unix/media/data/temp | 3 |
| /unix/media/data/temp/ | 4 |
+------------------------+-------+
I want the following to be a valid query:
mysql> select * from test1 where name='C:\\media\data\temp\';
I know that I can instead use
mysql> select * from test1 where name='C:\\\\media\\data\\temp\\';
But I am building this query using my_snprintf(), so there instead I have to use
C:\\\\\\\\media\\\\data\\\\temp\\\\
...and so on!
Is there a way to disable escape characters for a single MySQL query ?
You can disable backslash escapes by setting NO_BACKSLASH_ESCAPES in the SQL mode:
-- save mode & disable backslashes
SET #old_sql_mode=##sql_mode;
SET ##sql_mode=CONCAT_WS(',', ##sql_mode, 'NO_BACKSLASH_ESCAPES');
-- run the query
SELECT 'C:\\media\data\temp\';
-- enable backslashes
SET ##sql_mode=#old_sql_mode;
For tabular output in MySQL command line, the “boxing” around columns enables one column value to be distinguished from another. For non-tabular output (such as is produced in batch mode or when the --batch or --silent option is given), special characters are escaped in the output so they can be identified easily. Newline, tab, NUL, and backslash are written as \n, \t, \0, and \. The --raw option disables this character escaping.