Home > Database > Mysql Tutorial > How to assign SELECT results to a MySQL user variable using the SET statement?

How to assign SELECT results to a MySQL user variable using the SET statement?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-08-27 11:05:15
forward
1018 people have browsed it

如何使用 SET 语句将 SELECT 结果分配给 MySQL 用户变量?

In order to assign the SELECT result to a user variable using the SET statement, we need to write the SELECT statement as a subquery within parentheses. The condition is that the SELECT statement must return a single value. For easy understanding, we are using the data from the "Bid" table as follows -

mysql> select * from Tender;
+----+---------------+--------------+
| Sr | CompanyName   | Tender_value |
+----+---------------+--------------+
| 1  | Abc Corp.     | 250.369003   |
| 2  | Khaitan Corp. | 265.588989   |
| 3  | Singla group. | 220.255997   |
| 4  | Hero group.   | 221.253006   |
| 5  | Honda group   | 225.292266   |
+----+---------------+--------------+
5 rows in set (0.04 sec)
Copy after login

Now, in the following query, we are setting the maximum value of ender_value (rounded to two decimal places) as variable @Val_Max .

mysql> SET @Val_Max = (SELECT ROUND(MAX(tender_value),2) from tender);
Query OK, 0 rows affected (0.00 sec)

mysql> Select @Val_Max;
+----------+
| @Val_Max |
+----------+
| 265.59   |
+----------+
1 row in set (0.00 sec)
Copy after login

The above is the detailed content of How to assign SELECT results to a MySQL user variable using the SET statement?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template