mysql临时变量吗_MySQL临时变量赋值?

您可以使用SET命令进行临时变量分配。

语法如下SET @anyVariableName=(SELECT yourColumnName FROM yourTableName WHERE yourCondition);

为了理解上述语法,让我们创建一个表。创建表的查询如下mysql> create table tempVariableAssignment

-> (

-> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,

-> Name varchar(20),

-> Age int

-> );

现在使用插入命令在表中插入一些记录mysql> insert into tempVariableAssignment(Name,Age) values('John',25);

mysql> insert into tempVariableAssignment(Name,Age) values('Carol',26);

mysql> insert into tempVariableAssignment(Name,Age) values('Sam',28);

mysql> insert into tempVariableAssignment(Name,Age) values('David',19);

mysql> insert into tempVariableAssignment(Name,Age) values('Bob',23);

使用select语句显示表中的所有记录。

查询如下mysql> select *from tempVariableAssignment;

以下是输出+----+-------+------+

| Id | Name   Age   |

+----+-------+------+

| 1 | John   | 25   |

| 2 | Carol  | 26   |

| 3 | Sam    | 28   |

| 4 | David  | 19   |

| 5 | Bob    | 23   |

+----+-------+------+

5 rows in set (0.00 sec)

这是MySQL临时变量分配的查询mysql> set @findAge=(select Age from tempVariableAssignment where Id=4);

显示变量@findAge的值。

查询如下mysql> select @findAge;

以下是输出+----------+

| @findAge |

+----------+

| 19       |

+----------+

1 row in set (0.01 sec)

这是一个替代查询mysql> select Age INTO @anotherAge

-> from tempVariableAssignment where Id=4;

显示变量@anotherAge的值。

查询如下mysql> select @anotherAge;

以下是输出+-------------+

| @anotherAge |

+-------------+

| 19          |

+-------------+

1 row in set (0.00 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值