
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
What happens if I will assign a value to a MySQL user variable using a statement that returns multiple rows?
In case, if we will assign a value to a user variable using a statement that returns multiple rows then the value from the last row would be saved in that user variable because user variables can save the only single value. Following the example, in which we are using data from table ‘Tender’, will exhibit 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)
Above result set shows the data from table ‘Tender’. Now we will assign the values in
column ‘companyname’ in the variable @name as follows −
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 this variable, it gives only the name of the company which was at last row. It is because of user variable can store only single value.
mysql> Select @name; +-------------+ | @name | +-------------+ | Honda group | +-------------+ 1 row in set (0.00 sec)
- Related Articles
- What MySQL returns if sub-query, used to assign new values in the SET clause of UPDATE statement, returns multiple rows?
- What happens if MySQL query returns no rows?
- How can we use SET statement to assign a SELECT result to a MySQL user variable?
- What MySQL returns if sub-query, used to assign new values in the SET clause of UPDATE statement, returns no rows?
- How can we assign a bit value as a number to a user variable?
- How do I assign a dictionary value to a variable in Python?
- What happens if I will delete a row from MySQL parent table?
- How can I create a MySQL stored procedure that returns multiple values from a MySQL table?
- Storing value from a MySQL SELECT statement to a variable?
- What MySQL ASCII() function returns if I will provide NULL to it?
- What MySQL returns if I insert invalid value into ENUM?
- How can I set 0 if a query returns a null value in MySQL?
- What happens if I will add a UNIQUE constraint on the same column for multiple times?
- How to assign multiple values to a same variable in Python?
- What happens if I use both G and semicolon (;) termination symbol with a single MySQL statement?

Advertisements