静音变量分配

问题描述 投票:0回答:1

[mysql中的mysql-client使用时,如何仅使变量赋值?

我有一个文件sql.sql

select id := id from companies where name="StackExchange";
select profit, companyID from profits where companyID=@id
select easter from egg where easterEgg=@id

如果运行mysql < sql.sql,我会得到

@id = id
12

profit   companyID
------   ---------
-12000   12

easter
------
egg
expires
April

我想要除变量分配行以外的所有内容:

profit   companyID
------   ---------
-12000   12

easter
------
egg
expires
April

我想避免多次重复编写代码

select profit, companyID from profits where companyID=(select id from companies where name="StackExchange")
select easter from egg where easterEgg=(select id from companies where name="StackExchange")
select something_else from someTable where this_column=(select id from companies where name="StackExchange")
mysql sql client
1个回答
0
投票

尝试使用临时变量,因为它会将结果复制到会话变量中,所以不会输出结果。

SELECT id INTO @id FROM companies WHERE name="StackExchange";
SELECT profit, companyID FROM profits WHERE companyID=@id;
SELECT easter FROM egg WHERE easterEgg=@id;
© www.soinside.com 2019 - 2024. All rights reserved.