如何使用MySQL将查询结果存储在变量中?
要将查询结果存储在MySQL变量中,请使用SET命令。语法如下-
SET @anyVariableName = ( yourQuery);
为了理解上述概念,让我们创建一个表。以下是创建表的查询-
mysql> create table QueryResultDemo −> ( −> Price int −> );
现在让我们将一些记录插入表中。以下是插入记录的查询-
mysql> insert into QueryResultDemo values(100); mysql> insert into QueryResultDemo values(20); mysql> insert into QueryResultDemo values(200); mysql> insert into QueryResultDemo values(80);
在select语句的帮助下显示表中的所有记录。显示所有记录的查询如下-
mysql> select *from QueryResultDemo;
以下是输出-
+-------+ | Price | +-------+ | 100 | | 20 | | 200 | | 80 | +-------+ 4 rows in set (0.00 sec)
现在,您可以借助SET命令将查询结果设置为变量。查询如下。
mysql> Set @TotalPrice = (select sum(Price) from QueryResultDemo);
使用SELECT语句检查存储在变量“TotalPrice”中的值是什么-
mysql> select @TotalPrice;
以下是输出-
+-------------+ | @TotalPrice | +-------------+ | 400 | +-------------+ 1 row in set (0.00 sec)
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练