开发者

Saving commands for later re-use in MySQL?

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 writ开发者_JAVA百科ing out


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


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 take it the answer you are looking for isn't 'stored procedures'...?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜