
- 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
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 -
select @anyVariableName;
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)
- Related Articles
- 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?
- 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?
- Store a variable with the result of a MySQL SELECT CASE?
- 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 sum 3 different values in a column displaying total of each value in result set?
- MySQL query to return a string as a result of IF statement?
- How to declare a variable in MySQL for a normal query?
- Set MySQL select in a custom variable
- Calculating percentage in a MySQL query and round off the result
- MongoDB query to set user defined variable into query?
- How to get file extension of file as a result of MySQL query?

Advertisements