This question is similar to what I'm asking: Is there a way to create an SQL "alias"?
But not quite the same - I'm learning mysql and am going to be running the select command a lot as I screw with different commands. Is there a way I can alias something like:
select * from testdb
to
ls
Or something equally similar? Ideally, I'd be able to alias "select * from" to something like "show" and then just type "show testdb".
SQL aliases are used to give a table, or a column in a table, a temporary name. Aliases are often used to make column names more readable. An alias only exists for the duration of that query. An alias is created with the AS keyword.
The alias syntax The syntax for creating an alias is easy. You type the word "alias", followed by the name you want to give the alias, stick in an = sign and then add the command you want it to run – generally enclosed in single or double quotes. Single word commands like "alias c=clear" don't require quotes.
Advantages of MySQL AliasesIt makes the column or table name more readable. It is useful when you use the function in the query. It can also allow us to combines two or more columns. It is also useful when the column names are big or not readable.
Standard SQL disallows references to column aliases in a WHERE clause.
Three ideas come to mind:
a stored procedure, so you'd end up doing "call show('testdb')"
a shell alias, so you run "show testdb" have it map to "mysql -e 'select * from testdb'"
suck it up and type the command.
using alias with function can help you pass parameter to a alias like
example query = select * from testdb where param = 'test param'
created alias = sftb test param
to create such alias you can write something like this in your bashrc file.alias sftb='function sftb(){ mysql -e "select * from testdb where param = '$1'"; };sftb'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With