- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 MySQL select in a custom variable
Let us first create a table −
mysql> create table DemoTable2013 -> ( -> Name varchar(20) -> ); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2013 values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2013 values('David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2013 values('Mike'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable2013 values('Sam'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable2013 values('Bob'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2013;
This will produce the following output −
+-------+ | Name | +-------+ | Chris | | David | | Mike | | Sam | | Bob | +-------+ 5 rows in set (0.00 sec)
Here is the query to set MySQL SELECT statement in a custom variable −
mysql> set @query := (select count(*) from DemoTable2013); Query OK, 0 rows affected (0.00 sec) mysql> select Name -> from DemoTable2013 -> where (@query := @query-1) = 0;
This will produce the following output −
+------+ | Name | +------+ | Bob | +------+ 1 row in set (0.00 sec)
- Related Articles
- Set custom messages by working with MySQL IF Statements and SELECT in a user-defined variable
- MySQL query to find a value in a set of values separated by comma in a custom variable
- How can we use SET statement to assign a SELECT result to a MySQL user variable?
- Select into a user-defined variable with MySQL
- Storing value from a MySQL SELECT statement to a variable?
- Perform MySQL SELECT INTO user-defined variable
- Set custom Auto Increment with ZEROFILL in MySQL
- Set multiple values for custom columns in MySQL?
- Set custom messages for enum values in MySQL
- Select with set order in MySQL
- How to write a valid MySQL query and update with a custom variable?
- Store a variable with the result of a MySQL SELECT CASE?
- Set a custom value for NULL or empty values in MySQL
- Set the result of a query to a variable in MySQL?
- Can we set a single value in MySQL SELECT IN()?

Advertisements