- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
- 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?
- Can we use SELECT NULL statement in a MySQL query?
- How to assign the result of a MySQL query into a variable?
- Can we assign a reference to a variable in Python?
- 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?
- Set user variable from result of query in MySQL?
- Storing value from a MySQL SELECT statement to a variable?
- What happens if I will assign a value to a MySQL user variable using a statement that returns multiple rows?
- How can we use a MySQL subquery with INSERT statement?
- Select into a user-defined variable with MySQL
- How can we change MySQL user password by using the SET PASSWORD statement?