Found 4381 Articles for MySQL

Why is it not recommended to use the mixture of quoted as well as unquoted values in MySQL IN() function’s list?

George John
Updated on 30-Jul-2019 22:30:21

97 Views

Actually, MySQL has different comparison rules for quoted values such as strings and unquoted values such as numbers. On mixing the quoted and unquoted values in IN() function list may lead to the inconsistent result set. For example, we must not write the query with IN() function like below − Select Salary from employee where ID IN(1,’a’,2,3,’c’) Instead of this the better way to write the above query is as follows − Select Salary from employee where ID IN(‘1’,’a’,’2’,’3’,’c’)

How can I get the output based on comparison done with column’s name using MySQL IN() function?

Monica Mona
Updated on 22-Jun-2020 06:38:53

124 Views

In this scenario, we need to use the name of the column as ‘Expression’ which will then be compared with the values in the list. If a column has value/s matched within the list,  the output would be produced. For understanding it, consider the example from employee table having the following data −mysql> Select * from Employee; +----+--------+--------+ | ID | Name   | Salary | +----+--------+--------+ | 1  | Gaurav | 50000  | | 2  | Rahul  | 20000  | | 3  | Advik  | 25000  | | 4  | Aarav  | 65000  | | 5  | Ram    | 20000  | ... Read More

How can I use MySQL IN() function to compare row constructors?

Anjana
Updated on 22-Jun-2020 06:38:09

195 Views

We can also use IN() function to compare row constructors. Consider the following example to make it clearer −mysql> Select (10,2) IN ((5,10),(10,2),(2,10),(100,100)); +--------------------------------------------+ | (10,2) IN ((5,10),(10,2),(2,10),(100,100)) | +--------------------------------------------+ | 1                                          | +--------------------------------------------+ 1 row in set (0.00 sec) mysql> Select (10,2) IN ((5,10),(2,10),(100,100)); +-------------------------------------+ | (10,2) IN ((5,10),(2,10),(100,100)) | +-------------------------------------+ | 0                                   | +-------------------------------------+ 1 row in set (0.00 sec)

How can we write MySQL handler in a stored procedure?

Srinivas Gorla
Updated on 22-Jun-2020 06:29:16

424 Views

Whenever an exception occurred in MySQL stored procedure, it is very important to handle it by throwing a proper error message. Suppose, if we do not handle the exception, there would be a chance to fail application with that certain exception in a stored procedure. MySQL provides a handler to handle the exception in the stored procedure. Followings are the four kinds of MySQL handlers which can be used in a stored procedure −DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SELECT 'got an error';The above handler will throw an error message and continues the execution.DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET got_error=1;The above ... Read More

In MySQL, how can we declare a handler while handling errors?

Nishtha Thakur
Updated on 22-Jun-2020 06:33:33

652 Views

It is very important to handle the errors by throwing a proper error message. MySQL provides a handler to handle the error. We can declare a handler with the help of the following syntax −Syntax of handlerDECLARE handler_action FOR condition_value statement;The above syntax shows that we need to use DECLARE HANDLER statement to declare a handler. If a condition whose value matches the condition_value then MySQL will execute the statement and continue or exit the current code block based on the action. Followings are the three major things in the above syntax −Handler_action is of two types and can accept one ... Read More

How can we handle a result set inside MySQL stored procedure?

Smita Kapse
Updated on 22-Jun-2020 06:37:40

1K+ Views

We can use a cursor to handle a result set inside a stored procedure. Basically a cursor allows us to iterate a set of rows returned by a query and process each row accordingly.To demonstrate the use of CURSOR within MySQL stored procedure, we are creating the following stored procedure which is based on the values, as shown below, of the table named ‘student_info’ −mysql> Select * from student_info; +-----+---------+----------+------------+ | id  | Name    | Address  | Subject    | +-----+---------+----------+------------+ | 101 | YashPal | Amritsar | History    | | 105 | Gaurav  | Jaipur   | Literature ... Read More

How Can MySQL LOOP statement be used in a stored procedure?

Abhinanda Shri
Updated on 22-Jun-2020 06:39:54

533 Views

MySQL provides us a LOOP statement that executes a block of code repeatedly along with an added flexibility of using a loop label. We have the following two statements that allow us to control the loop −LEAVE statementIt allows us to exit the loop immediately without waiting for checking the condition.Iterate statementIt allows us to skip the entire code under it and start a new iteration.To demonstrate the use of LOOP statement with stored procedures, the following is a stored procedure which constructs a string with even numbers like 2, 4, 6, 8 etc. −mysql> Delimiter // mysql> CREATE PROCEDURE LOOP_loop() ... Read More

In MySQL, how IN() comparison function works?

Rama Giri
Updated on 22-Jun-2020 06:42:32

155 Views

Basically, IN() comparison function checks whether a value is within a set of values or not. If the value is within a set of values then it returns 1 otherwise 0. Its syntax can be as follows;Expression IN (val1, val2, …, valN)Here, The expression is the value that is to be searched within the set of N values within IN list.Val1, val2, …, valN is the set of N values, forms the IN list, from which the search happens.Examplemysql> Select 100 IN (50, 100, 200, 400, 2000); +------------------------------+ | 100 IN (50, 100, 200, 400, 2000) | +------------------------------+ |   ... Read More

What is the use of MySQL IS and IS NOT operator?

Ankith Reddy
Updated on 22-Jun-2020 06:41:52

324 Views

In MySQL, both IS and IS NOT operators are used to test a value against a Boolean value.The syntax of IS operator can be as follows −Val IS Boolean_valHere Val is the value that we want to test against Boolean value.Boolean_val is the Boolean value against which the value would be tested and it can be TRUE, FALSE or UNKNOWN.The syntax of IS NOT operator can be as follows −Val IS NOT Boolean_valHere Val is the value that we want to test against Boolean value.Boolean_val is the Boolean value against which the val would be tested and it can be TRUE, FALSE or UNKNOWN.Following MySQL statements will demonstrate ... Read More

How MySQL NULL-safe equal operator performs when used with row comparisons?

Fendadis John
Updated on 22-Jun-2020 06:20:25

134 Views

When we use NULL-safe operator with row comparisons like (A, B) (C, D) then its performance is equivalent to (A C) AND (B D). Following example will demonstrate it −mysql> Select (100,50) (50,100); +-----------------------+ | (100,50) (50,100) | +-----------------------+ |                     0 | +-----------------------+ 1 row in set (0.00 sec) mysql> Select (100,50) (100,50); +-----------------------+ | (100,50) (100,50) | +-----------------------+ |                     1 | +-----------------------+ 1 row in set (0.00 sec)The above result sets show how NULL-safe operators can be used with row comparison.

Advertisements