Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2023-09-01 13:33:06
forward
1175 people have browsed it

如果我使用返回多行的语句为 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)
Copy after login

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)
Copy after login

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)
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!