Solved

IF statement in WHERE clause of MySQL Query

Posted on 2013-06-21
9
359 Views
Last Modified: 2013-07-01
I'm trying to pull absence requests to a page and in the query, we store the return to work date...

If it is a partial day off, I want to display the return date that is stored in the database, but if it is not a partial day< I want to pull one day less than the return date.

So for example if I took off from 6-18 and my return date is 6-21, on todays calendar view, I will not be listed...however if I took off on 6-21 for three hours, then I should display on the calendar.

I'm not sure where I'm at with the query below, all I know is it doesn't work.

$getAbsences = mysql_query("SELECT respid, startDate, returnDate FROM fm_calendar_absenceRequest WHERE SUBSTRING(startDate,1,10) <= '".date("Y-m-d", strtotime($dayChoice))."' 
AND 
	IF(partialDay = 1(SUBSTRING(returnDate,1,10) >= '".date("Y-m-d", strtotime($dayChoice))."'))
	IF(partialDay = 1(SUBSTRING(returnDate,1,10) >= '".date("Y-m-d", strtotime($dayChoice), '-1 day')."')) AND hidden = '0' AND requestApproved = '1'") or die("Check Time: " . mysql_error());

Open in new window

0
Comment
Question by:t3chguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39267845
seems to me that you need a case expression within the select clause (this is the sql IF equivalent). Currently you are trying to embed some if conditions into the where clause (and IF isn't a sql thing).

I'm assuming partialDay is a field in the table.
$getAbsences = mysql_query("SELECT respid, startDate, 
 CASE WHEN partialDay = 1 THEN SUBSTRING(returnDate - INTERVAL 1 DAY, 1, 10) 
      WHEN partialDay = 0 THEN SUBSTRING(returnDate, 1, 10) 
 END  as returnDate 
 FROM fm_calendar_absenceRequest 
 WHERE SUBSTRING(startDate,1,10) <= '".date("Y-m-d", strtotime($dayChoice))."' 
 AND hidden = '0' AND requestApproved = '1'") or die("Check Time: " . mysql_error());

-- as straight sql it would look like this:

SELECT respid, startDate, 
 CASE WHEN partialDay = 1 THEN SUBSTRING(returnDate - INTERVAL 1 DAY, 1, 10) 
      WHEN partialDay = 0 THEN SUBSTRING(returnDate, 1, 10) 
 END  as returnDate 
 FROM fm_calendar_absenceRequest 
 WHERE SUBSTRING(startDate,1,10) <= '2013-06-24' 
 AND hidden = '0' AND requestApproved = '1'

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39267849
you might prefer to use 'else'
SELECT respid, startDate, 
 CASE WHEN partialDay = 1 THEN SUBSTRING(returnDate - INTERVAL 1 DAY, 1, 10) 
      ELSE SUBSTRING(returnDate, 1, 10) 
 END  as returnDate 
 FROM fm_calendar_absenceRequest 
 WHERE SUBSTRING(startDate,1,10) <= '2013-06-24' 
 AND hidden = '0' AND requestApproved = '1'

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39268078
See if this article can give you some clues about it.  If you want to post a few examples of the inputs and expected outputs, we may be able to give you a tested-and-working example.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Author Comment

by:t3chguy
ID: 39271245
PortletPaul,

The query you had suggested returns ALL events prior to 6/24, instead of the 22 absences that it should be returning.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39271320
>> 22 absences that it should be returning.
I was trying to answering the following really:

"If it is a partial day off, I want to display the return date that is stored in the database, but if it is not a partial day< I want to pull one day less than the return date."
and for this you need something like the case expression I provided

To figure out "22 absences that it should be returning" would I guess require a change to the where clause - but I don't know you data.  How does one arrive at the 22 expected absences? (I can help translate that into sql if needed).

e.g. should the where clause be something like?
 WHERE SUBSTRING(startDate,1,10) >= '2013-06-01'
AND hidden = '0' AND requestApproved = '1'

bottom line: I'm guessing how to arrive at the 22 expected results
- & I didn't know that was part of the question
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39271338
If you want to post a few examples of the inputs and expected outputs, we may be able to give you a tested-and-working example.
http://sscce.org/

Really, it's all about the data.
0
 
LVL 1

Author Comment

by:t3chguy
ID: 39271346
Sure, sorry.

Essentially we are trying to display the absences for that particular day.

There are different scenarios all of which I need to account for.

1.  If an employee took a partial day (which is a flag in the table), they need to show up for being off today.

2.  If an employees return date is today, then they shouldn't be, which is where I was trying to use the CASE statement for if its a partial day, then show up, but if its not a partial day, then somehow exclude that record from the result date, since they are back to work today.

3.  If an employee is out for an extended period of time, for example started their vacation on 6/22 and returns 7/1, they should appear on this list even though the start and end date is out of this range.

The report is currently done on another server, but the guy before me ran multiple queries and used PHP to help manipulate the data, where I was hoping for an all MySQL solution, but maybe thats not possible?  I'm not sure.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39271479
There are different scenarios all of which I need to account for.
If you can post some of the test data set that shows these different scenarios, we can almost certainly help you further.  Descriptions of the test data are almost always useless; the actual data is all we need.

An example of the data might take the form of a short script that created a temporary table and inserted the data into the table.  The SQL INSERT statements need not be many or complicated - just enough data to illustrate each of the scenarios you want to treat differently.

Armed with a little script that does those things, we can almost certainly show you tested and working code examples.  Thanks, ~Ray
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39271671
on today's calendar view...
does this help?
SELECT respid, startDate, 
 CASE WHEN partialDay <> 1 THEN SUBSTRING(returnDate - INTERVAL 1 DAY, 1, 10) 
      ELSE SUBSTRING(returnDate, 1, 10)  -- i.e. if partial day use stored returnDate
 END  as returnDate 
 FROM fm_calendar_absenceRequest 
 WHERE hidden = '0' AND requestApproved = '1'
 AND (
       (
       CURDATE() between startDate and returnDate  -- i.e. if partial day use stored returnDate
       and partialDay = 1
       )
     OR
       (
       CURDATE() between startDate and (returnDate - INTERVAL 1 DAY)
       and partialDay <> 1
       )

Open in new window

0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

732 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