views:

35

answers:

4

What would be the equivalant in MySQL for:

  1. Saving a command for later reuse.

    eg: alias command1='select count(*) from sometable;' Where then I simply type command 1 to get the count for SomeTable.

  2. Saving just a string, or rather part of a command.

    eg: select * from sometable where $complex_where_logic$ order by attr1 desc; WHere $complex_where_logic$ is something I wish to save and not have to keep writing out

A: 

This "template" feature could be part of client tools. Basically I use Toad. It has record macros feature. I think it is possible to do.

I wish I could use Toad on Linux.
Zombies
A: 

I take it the answer you are looking for isn't 'stored procedures'...?

Paddy
That would sort of work for 1. But it seems like there should be something else that would simply save the string. I guess this type of thing is accomplished from a GUI front end.
Zombies
+1  A: 

Another approach is to create a view with your $complex_where_logic$ and query the view instead of the tables:

CREATE VIEW my_view AS SELECT * FROM sometable WHERE $complex_where_logic$

SELECT my_column FROM my_view ORDER BY some_column

Whenever you query a view, you always get the up-to-date data. Internally, MySQL runs the SELECT given in the CREATE VIEW statement and queries the results in order to obtain the results of your current SELECT. Therefore, a view does not improve performance compared to a single query. There a two main advantages in using views:

  • you have simpler SELECT statements since you do not have to type complex WHERE or JOIN Syntax again and again
  • you can use it to control user privileges, e.g. give a user access to a view but not to the original tables; this is not useful in your example, but - for example - you can think of views containing aggregate data only
titanoboa
A: 

I found that the best solution for this is just any rich GUI for SQL queries (TOAD, mysql query browser, etc). They offer the ability to save commands and browse them and well, of course, much more.

Zombies