[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")
尝试使用临时变量,因为它会将结果复制到会话变量中,所以不会输出结果。
SELECT id INTO @id FROM companies WHERE name="StackExchange";
SELECT profit, companyID FROM profits WHERE companyID=@id;
SELECT easter FROM egg WHERE easterEgg=@id;