
- 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
How can we use SET statement to assign a SELECT result to a MySQL user variable?
For using the SET statement to assign a SELECT result to a user variable we need to write the SELECT statement as a subquery within parentheses. The condition is that the SELECT statement must have to return a single value. To make it understand we are using the data from ‘Tender’ table which is 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, after rounding it up to 2 decimal point, of tender_value to the 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)
- Related Articles
- How can we assign a bit value as a number to a user variable?
- Assign an SQL result to variable from prepared statement in MySQL?
- How to assign the result of a MySQL query into a variable?
- Can we use SELECT NULL statement in a MySQL query?
- How can we assign a function to a variable in JavaScript?
- How can we use MySQL SELECT statement to count number of rows in a table?
- How can we set up a MySQL User account by using INSERT INTO statement?
- How can we set up a MySQL User account by using SQL GRANT statement?
- Can we assign a reference to a variable in Python?
- Set user variable from result of query in MySQL?
- What happens if I will assign a value to a MySQL user variable using a statement that returns multiple rows?
- Storing value from a MySQL SELECT statement to a variable?
- How can we change MySQL user password by using the SET PASSWORD statement?
- Select into a user-defined variable with MySQL
- How can we handle a result set inside MySQL stored procedure?

Advertisements