Monday, April 5

Executing mysql instructions from the shell

I bet many already know this, but whatever ...

the -e option for the mysql command allows us to execute an instruction without entering into the mysql itself, for example.

$ mysql -uroot -ppassword -e "SELECT * FROM `Database`.`Table`"

this gives us an excellent way of printing results into a file, for example:

$ mysql -uroot -ppassword -e "USE Database; SELECT id, name FROM Table WHERE arbirtary_id IS NULL;" > nullRecords.txt

and with a simple parser we can use this data to get rid of inconsistent information or obsolete data.