Oh dear, it has been over a year since my last post. I shouldn't be so lazy. Enough said, lets blow the dust of this blog and start being more productive again.

If you want to execute a sql file directly from Sublime Text and want to see the result (or the error) in the console you need to create a build system. This is easily done with Tools > Build System > New Build System... Then paste this JSON:

{
    "cmd": ["/opt/lampp/bin/mysql", "-u", "root", "-e", "source $file", "-t"],
    "selector": "source.sql"
}

A little bit of explanation. I'm using lampp (XAMPP for Linux), so the command is the path to my mysql binary. If you have a symbolic link or your mysql installation is somewhere else just replace it. The -u attribute stands for the user which is in my case root. I haven't set a password  and a host because I'm running this on my local machine. You can add a password with -p and a host with -h. The -e stands for execute, source that the statement comes from a file and $file is a Sublime variable which will be replaced with the current file you're editing. Last but not least the -t option formats the output of a select in a beautifully designed table. You can add -D db_name to automatically select a database, however I prefer the USE db_name at the beginning of a sql file. The selector is used when you set your build system to automatic to find the correct build system for your file. Now save it, open a sql file or create a new one. With Ctrl + B you execute the build command and the output should be printed into the console. (View > Show Console)

Example: USE awesomedb; SELECT * FROM users;

Output:

+----+-------------------+--------------------+--------+
| id | email             | password           | active |
+----+-------------------+--------------------+--------+
| 28 | user1@email.com   | asdfasdfasdfasdffg |      1 |
| 29 | user2@example.com | asfasdfasdfasdfasd |      1 |
| 45 | user3@random.com  | adfasdfasdfasdfasd |      0 |
+----+-------------------+--------------------+--------+

Sources
http://seant23.wordpress.com/2011/09/03/sublime-text-2-execute-mysql-query/
http://dev.mysql.com/doc/refman/5.5/en/mysql-command-options.html
http://sublimetext.info/docs/en/reference/build_systems.html