
- 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 custom messages by working with MySQL IF Statements and SELECT in a user-defined variable
Let us first create a table −
mysql> create table DemoTable2008 ( Value int ); Query OK, 0 rows affected (10.59 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2008 values(10); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable2008 values(20); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable2008 values(30); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable2008 values(-31); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable2008 values(-28); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable2008;
This will produce the following output −
+-------+ | Value | +-------+ | 10 | | 20 | | 30 | | -31 | | -28 | +-------+ 5 rows in set (0.00 sec)
Here is the query to first work with a user-defined variable and taking input as a SELECT −
mysql> set @value:=(select sum(Value) from DemoTable2008); Query OK, 0 rows affected (0.03 sec) mysql> select if(@value > 0, 'Value is greater than 0','Not greater than') as Result;
This will produce the following output −
+-------------------------+ | Result | +-------------------------+ | Value is greater than 0 | +-------------------------+ 1 row in set (0.00 sec)
- Related Articles
- Select into a user-defined variable with MySQL
- Perform MySQL SELECT INTO user-defined variable
- MySQL ORDER BY with numeric user-defined variable?
- Set MySQL select in a custom 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?
- Set custom messages for enum values in MySQL
- User defined and custom exceptions in Java
- MongoDB query to set user defined variable into query?
- Updating a MySQL table row column by appending a value from user defined variable?
- User-defined Custom Exception in C#
- Set custom messages on the basis of a column with student marks in MySQL
- How to set different auto-increment ids for two tables with a user-defined variable?
- MySQL IF() to display custom YES or NO messages
- How Can we permanently define user-defined variable for a client in MySQL?

Advertisements