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 -
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!