
- 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
Perform MySQL SELECT INTO user-defined variable
Let us first create a table −
mysql> create table DemoTable1483 -> ( -> Salary int -> ); Query OK, 0 rows affected (0.41 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1483 values(100); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1483 values(500); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1483 values(400); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1483;
This will produce the following output −
+--------+ | Salary | +--------+ | 100 | | 500 | | 400 | +--------+ 3 rows in set (0.00 sec)
Following is the query to perform select into user-defined variable −
mysql> select sum(Salary) into @TotalSalary from DemoTable1483; Query OK, 1 row affected (0.00 sec)
Now display the value of variable −
mysql> select @TotalSalary;
This will produce the following output −
+--------------+ | @TotalSalary | +--------------+ | 1000 | +--------------+ 1 row in set (0.00 sec)
- Related Articles
- Select into a user-defined variable with MySQL
- MySQL ORDER BY with numeric user-defined variable?
- MongoDB query to set user defined variable into query?
- Set custom messages by working with MySQL IF Statements and SELECT in a user-defined variable
- Set user-defined variable with table name in MySQL prepare statement?
- In MySQL, why a client cannot use a user-defined variable defined by another client?
- How Can we permanently define user-defined variable for a client in MySQL?
- Updating a MySQL table row column by appending a value from user defined variable?
- Using User-Defined Variables in MySQL
- How can we store a value in user-defined variable?
- Perform multiple inserts with INSERT INTO SELECT and UNION in MySQL
- Swap two Strings without using third user defined variable in Java
- User-defined variables vs Local Variables in MySQL?
- System variables vs User-defined variables in MySQL?
- Perform MySQL SELECT on dates inserted into the table as VARCHAR values

Advertisements