select*fromTender;+----+---------------+------ --------+|Sr|CompanyName |Tender_value|+----+---------------+------------ --+|1 |AbcC"/> select*fromTender;+----+---------------+------ --------+|Sr|CompanyName |Tender_value|+----+---------------+------------ --+|1 |AbcC">

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?

WBOY
WBOYforward
2023-08-27 11:05:15970browse

如何使用 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)

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)

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete