

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 custom messages by working with MySQL IF Statements and SELECT in a user-defined variable
<p>Let us first create a table −</p><pre class="result notranslate">mysql> create table DemoTable2008 ( Value int ); Query OK, 0 rows affected (10.59 sec)</pre><p>Insert some records in the table using insert command −</p><pre class="result notranslate">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)</pre><p>Display all records from the table using select statement −</p><pre class="prettyprint notranslate">mysql> select * from DemoTable2008;</pre><p>This will produce the following output −</p><pre class="result notranslate">+-------+ | Value | +-------+ | 10 | | 20 | | 30 | | -31 | | -28 | +-------+ 5 rows in set (0.00 sec)</pre><p>Here is the query to first work with a user-defined variable and taking input as a SELECT −</p><pre class="prettyprint notranslate">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;</pre><p>This will produce the following output −</p><pre class="result notranslate">+-------------------------+ | Result | +-------------------------+ | Value is greater than 0 | +-------------------------+ 1 row in set (0.00 sec)</pre>
- Related Questions & Answers
- Select into a user-defined variable with MySQL
- MySQL ORDER BY with numeric user-defined variable?
- Perform MySQL SELECT INTO 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?
- User defined and custom exceptions in Java
- Set custom messages for enum values in MySQL
- User-defined Custom Exception in C#
- MongoDB query to set user defined variable into query?
- Updating a MySQL table row column by appending a value from user defined variable?
- MySQL IF() to display custom YES or NO messages
- Set custom messages on the basis of a column with student marks in MySQL
- MySQL CREATE USER with a variable?
- How to set different auto-increment ids for two tables with a user-defined variable?
Advertisements