?
Solved

mysql nested IF statement in SELECT

Posted on 2011-10-12
4
Medium Priority
?
661 Views
Last Modified: 2012-05-12
Hi,

I have a problem getting a nested IF clause to work in my mysql SELECT statement.
Please can you have a look at the attached code particularly the line below which is not working
(IF spContract_start > (IF (start_date< '2011-04-04', '2011-04-04', start_date) as StartUnavail),spContract_start,StartUnavail) as start,

What should be happening is the follows

if start_date < '2011-04-04' use '2011-04-04' else use the value 'start_date'  - lets call ths used value StartUnavail

if spContract_start > StartUnavail calculated above use spContract_start else use StartUnavail.


Hope this makes sense. Thanks

 

SELECT tb2.start_date, tb2.end_date, tb2.hostel , tb2.room_no, tb4.project as project,
(IF spContract_start > (IF (start_date< '2011-04-04', '2011-04-04', start_date) as StartUnavail),spContract_start,StartUnavail) as start,
(IF (((end_date > '2011-07-03') OR (end_date IS NULL)),'2011-07-03',end_date) as EndUnavail)
FROM room_unavailable AS tb2 (LEFT OUTER JOIN room AS tb1 ON tb1.hostel = tb2.hostel AND tb1.room_no = tb2.room_no) CROSS JOIN hostel AS tb4 ON tb2.hostel = tb4.hostel_name
WHERE ((((tb2.start_date <= '2011-04-04') AND (tb2.end_date >='2011-04-04'))
OR ((tb2.start_date <= '2011-07-03') AND (tb2.end_date >='2011-07-03'))
OR ((tb2.start_date >= '2011-04-04') AND (tb2.end_date <='2011-07-03'))
OR ((tb2.start_date <= '2011-07-03') AND (tb2.end_date = '0000-00-00'))));

Open in new window

0
Comment
Question by:EICT
  • 2
  • 2
4 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 36954704
SELECT tb2.start_date, tb2.end_date, tb2.hostel , tb2.room_no, tb4.project as project,

Case When spContract_start >
(Case When start_date< '2011-04-04' then '2011-04-04' else start_date end case)
then spContract_start else StartUnavail end case as start,

Case When ((end_date > '2011-07-03') OR (end_date IS NULL)) then
'2011-07-03' else end_date as EndUnavail
FROM room_unavailable AS tb2 (LEFT OUTER JOIN room AS tb1 ON tb1.hostel = tb2.hostel AND tb1.room_no = tb2.room_no) CROSS JOIN hostel AS tb4 ON tb2.hostel = tb4.hostel_name
WHERE ((((tb2.start_date <= '2011-04-04') AND (tb2.end_date >='2011-04-04'))
OR ((tb2.start_date <= '2011-07-03') AND (tb2.end_date >='2011-07-03'))
OR ((tb2.start_date >= '2011-04-04') AND (tb2.end_date <='2011-07-03'))
OR ((tb2.start_date <= '2011-07-03') AND (tb2.end_date = '0000-00-00'))));
0
 

Author Comment

by:EICT
ID: 36954781
Thanks.
A quick question. StartUnavail should be the value of the nested case. The following seems a bit long (i.e. repeating the nested case).

Case When spContract_start >
(Case When start_date< '2011-04-04' then '2011-04-04' else start_date end case)
then spContract_start else (Case When start_date< '2011-04-04' then '2011-04-04' else start_date end case)
 end case as start,

if we called the nested case result A then it would look like this.

Case When spContract_start > (A) then spContract_start else (A) end case as start,



0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 1000 total points
ID: 36954803
select
X.start_date, X.end_date, X.hostel , X.room_no, X.project,
Case When X.spContract_start > X.StartUnavail then X.spContract_start else X.StartUnavail end case as start,
X.EndUnavail
FRom
(SELECT tb2.start_date, tb2.end_date, tb2.hostel , tb2.room_no, tb4.project as project,spContract_start,

Case When start_date< '2011-04-04' then '2011-04-04' else start_date end case as StartUnavail
,

Case When ((end_date > '2011-07-03') OR (end_date IS NULL)) then
'2011-07-03' else end_date as EndUnavail
FROM room_unavailable AS tb2 (LEFT OUTER JOIN room AS tb1 ON tb1.hostel = tb2.hostel AND tb1.room_no = tb2.room_no) CROSS JOIN hostel AS tb4 ON tb2.hostel = tb4.hostel_name
WHERE ((((tb2.start_date <= '2011-04-04') AND (tb2.end_date >='2011-04-04'))
OR ((tb2.start_date <= '2011-07-03') AND (tb2.end_date >='2011-07-03'))
OR ((tb2.start_date >= '2011-04-04') AND (tb2.end_date <='2011-07-03'))
OR ((tb2.start_date <= '2011-07-03') AND (tb2.end_date = '0000-00-00')))) ) X
0
 

Author Closing Comment

by:EICT
ID: 36954881
Thanks. Good idea to do a select within a select.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question