简体   繁体   English

为 mySQL 设置全局变量

[英]Setting global variable for mySQL

As I am working my SQL, for example if I have在我工作时,我的 SQL,例如,如果我有

select
concat(author_fname, ' ', author_lname)..
....
from...

The question that I have is when I do multiple queries, I have to type that concat command over and over again.我遇到的问题是,当我进行多个查询时,我必须一遍又一遍地键入该 concat 命令。

Is there a way that I can set a global variable let's say有没有办法可以设置一个全局变量让我们说

set
@full_name =(
    select
        concat(author_fname, ' ', 
        author_lname)
    from
        books
);

Now I only need to do:现在我只需要做:

select full_name 
from books;

But my set command does not work, I don't think declare is the way to go, is there a way to tackle this problem?但是我的 set 命令不起作用,我不认为 declare 是 go 的方法,有没有办法解决这个问题? Thank you so much!太感谢了!

You can use a view:您可以使用视图:

create view v_books as
    select concat(author_fname, ' ', author_lname) as full_name, b.*
    from books b;

You will see the column if you select from the view.如果您从视图中看到 select,您将看到该列。

Or use a generated column if you want it when selecting directly from the table:或者在直接从表中选择时使用生成的列:

alter table books add full_name varchar(255) generated always as
    (concat(author_fname, ' ', author_lname));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM