如何在查询中使用变量值

时间:2019-01-14 18:16:13

标签: mysql variables mysql-workbench

如下面的代码所示,我想将表导出到“ secure_file_priv”中的位置,因为它只是我手动复制并粘贴它,但我希望能够使用它的值。

USE Library;


show VARIABLES LIKE 'secure_file_priv';

set mylocation := (VARIABLES LIKE 'secure_file_priv');

SELECT 'Id', 'Name', 'Birthplace', 'Birthday', 'Gender'
UNION ALL
SELECT Id, Name, Birthplace, Birthday, Gender
    FROM Author
    INTO OUTFILE '/var/lib/mysql-files/author.csv'
    FIELDS TERMINATED BY ',' 
    OPTIONALLY ENCLOSED BY ''
    LINES TERMINATED BY '\n';

我希望能够使用'secure_file_priv'变量的值,而不是手动编写'/var/lib/mysql-files/author.csv'。

1 个答案:

答案 0 :(得分:0)

SET @mylocation = (SELECT @@secure_file_priv);

SELECT 'Id', 'Name', 'Birthplace', 'Birthday', 'Gender'
UNION ALL
SELECT Id, Name, Birthplace, Birthday, Gender
    FROM Author
    INTO OUTFILE @mylocation
    FIELDS TERMINATED BY ',' 
    OPTIONALLY ENCLOSED BY ''
    LINES TERMINATED BY '\n';