Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Sql Mysql-example MySQL Case In Where Clause

Related Tutorials


 
 

Share on Google+Share on Google+

MySQL Case In Where Clause

Advertisement
MySQL Case: If you want to implement a complex conditional construction then you can use MySQL case statement. It performs the work as programming type.

MySQL Case In Where Clause

     

This example illustrates how to use case statement where clause in MySQL database.  

MySQL Case: If you want to implement a complex conditional construction then you can use MySQL case statement. It performs the work as programming type.  The CASE statement  is used with two other keywords "WHEN" and "THEN". If the column data is matched with the "WHEN" data then it takes the corresponding "THEN" data. If no data is matched then the "ELSE" data is taken.

 

 

 

 

Table: employeeDetails

CREATE TABLE `employeedetails` ( 
`id` int(11) NOT NULL auto_increment, 
`empName` varchar(100) NOT NULL, 
`daysName` varchar(50) NOT NULL, 
PRIMARY KEY (`id`) 
)


Query to select all data:
SELECT * FROM employeedetails;

Output:

Using MySQL 'case' and 'where' clause:

SELECT daysName, CASE daysName 
  WHEN "Sunday" THEN "Holiday" 
  WHEN "Monday" THEN "Working Day" 
  WHEN "Tuesday" THEN "Working Day" 
  WHEN "Wednesday" THEN "Working Day" 
  WHEN "Thrusday" THEN "Working Day" 
  WHEN "Friday" THEN "Working Day" 
   WHEN "Saturday" THEN "Holiday" 
  ELSE "Illegal Argument" 
END AS "Status" 
FROM employeeDetails 
WHERE daysName='Sunday' 
OR daysName='Monday' OR daysName='Tuesday' 
OR daysName='Wednesday' OR daysName='Thrusday' 
OR daysName='Friday' OR daysName='Saturday';

The query above will exclude the row having value "January" in the daysName column from the result.

Output:

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 20, 2009

Related Tutorials

Discuss: MySQL Case In Where Clause  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com