Solved: How to execute a SQL query from shell

Post Reply
mister_v
Posts: 188
Joined: Thu Mar 04, 2010 9:19 pm

Solved: How to execute a SQL query from shell

Post by mister_v »

Hello,

How do I execute a SQL query directly from shell command line?

Thanks,
Last edited by mister_v on Wed Aug 22, 2012 3:18 pm, edited 1 time in total.
chris
Site Admin
Posts: 194
Joined: Mon Jul 21, 2008 9:52 am

Re: How to execute a SQL query from shell

Post by chris »

The easiest way I know is with the SQL-command.

Code: Select all

mysql -u user -p -e 'SELECT data FROM table;' database
If you want juts the data:

Code: Select all

mysql -u user -p'password' -sNe 'SELECT data FROM table;' database
-s : separates the values with only tabs
N : skips the column names

-p the password (without spaces, not even between the -p and the password )
else use --password='password.'
Post Reply