- 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
Set the result of a query to a variable in MySQL?
You can set the result of a query using select into command. The syntax is as follows.
select yourColumnName1 into @anyVariableName from yourTableName where yourColumnName2='anyValue';
Check the result is present in the variable or not using the select command. The syntax is as follows -
To understand the above syntax, let us first create a table. The query to create a table is as follows.
mysql> create table StudentInformation -> ( -> StudentId int, -> StudentName varchar(100), -> StudentAge int -> ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command. The query is as follows.
mysql> insert into StudentInformation values(1,'John',23); Query OK, 1 row affected (0.21 sec) mysql> insert into StudentInformation values(2,'Adam',24); Query OK, 1 row affected (0.17 sec) mysql> insert into StudentInformation values(3,'Bob',21); Query OK, 1 row affected (0.20 sec) mysql> insert into StudentInformation values(4,'Carol',20); Query OK, 1 row affected (0.17 sec) mysql> insert into StudentInformation values(5,'Mike',25); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement. The query is as follows.
mysql> select *from StudentInformation;
The following is the output.
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | John | 23 | | 2 | Adam | 24 | | 3 | Bob | 21 | | 4 | Carol | 20 | | 5 | Mike | 25 | +-----------+-------------+------------+ 5 rows in set (0.00 sec)
Here is the query to set the result of query into a variable.
mysql> select StudentAge into @yourAge from StudentInformation where StudentName='Adam'; Query OK, 1 row affected (0.03 sec)
Check what is stored in the variable @yourAge. The query is as follows.
mysql> select @yourAge;
The following is the output displaying age of Student Adam.
+----------+ | @yourAge | +----------+ | 24 | +----------+ 1 row in set (0.00 sec)
- Set user variable from result of query in MySQL?
- How to assign the result of a MySQL query into a variable?
- How to store Query Result in a variable using MySQL?
- Store a variable with the result of a MySQL SELECT CASE?
- How to store query result (a single document) into a variable?
- How can we use SET statement to assign a SELECT result to a MySQL user variable?
- MySQL query to find a value in a set of values separated by comma in a custom variable
- Write a MySQL query to check if field exists and then return the result set?
- MySQL query to return a string as a result of IF statement?
- Set MySQL select in a custom variable
- MySQL query to sum 3 different values in a column displaying total of each value in result set?
- Calculating percentage in a MySQL query and round off the result
- How to declare a variable in MySQL for a normal query?
- How to get file extension of file as a result of MySQL query?
- MongoDB query to set user defined variable into query?