Solved

IF statement in WHERE clause of MySQL Query

Posted on 2013-06-21
9
357 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
  • 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 109

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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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 109

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 109

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now