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

Home  >  Article  >  Database  >  What happens if I assign a value to a MySQL user variable using a statement that returns multiple rows?

What happens if I assign a value to a MySQL user variable using a statement that returns multiple rows?

WBOY
WBOYforward
2023-09-01 13:33:061208browse

如果我使用返回多行的语句为 MySQL 用户变量赋值,会发​​生什么情况?

In case, if we assign a value to a user variable using a statement that returns multiple rows, then the value of the last row will be saved in that user variable, because user variables can save only single value. In the following example, we are using the data from the table "Tender" and will show it -

Example

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)

The above result set shows the data from the "Tender" table. Now we will assign the value in the "companyname" column in the variable @name as shown below -

mysql> Select @name := companyname from tender;
+----------------------+
| @name := companyname |
+----------------------+
| Abc Corp.            |
| Khaitan Corp.        |
| Singla group.        |
| Hero group.          |
| Honda group          |
+----------------------+
5 rows in set (0.00 sec)

But, now when we refer to this variable, it only gives the company name of the last row. This is because user variables can only store a single value.

mysql> Select @name;
+-------------+
| @name       |
+-------------+
| Honda group |
+-------------+
1 row in set (0.00 sec)

The above is the detailed content of What happens if I assign a value to a MySQL user variable using a statement that returns multiple rows?. 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