简体   繁体   中英

Setting global variable for mySQL

As I am working my SQL, for example if I have

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.

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? 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.

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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