MySQL Forums
Forum List  »  General

IF in query
Posted by: Ronny Drechsler-Hildebrandt
Date: November 10, 2014 04:49AM

Hi

I am afraid I need your help to create a part of a query.
Situation. We have a small booking applikation which stores the booked data in two fields "from" and "till".
Example: A person books a room "from" 2014-11-27 "till" 2014-12-03.
Now I wolud like to read out "the workload" of the rooms per month.
The month will be provided as an varibale (PHP) $month_i_wanted_to_know

Therefore I need to make three differences in my query

1. Case:

"from" and "till" conatins the same month
So in MySql the question will something like:

"IF(MONTH(from) == MONTH(TILL) == $month_i_wanted_to_know)"

QUERY -> SELECT SUM(DATEDIFF(from,till)) AS AMOUNT


2. Case:
"from" contains the month for which I want to have the data and "till" is in the next or other month.
So the question will be something like that:

"IF(MONTH(from) == $month_i_wanted_to_know AND MONTH(till) > $month_i_wanted_to_know"

QUERY -> SELECT SELECT SUM(DATEDIFF(from,LAST_DAY(from))) AS AMOUNT

3. Case:
"till" contains the month for which I want to have the data and "from" is the month before.

So the question will be something like that:

"IF(MONTH(till) == $month_i_wanted_to_know AND MONTH(from) < $month_i_wanted_to_know"

QUERY -> SELECT SELECT SUM(DATEDIFF(YEAR(till)-MONTH(till)-01,(till))) AS AMOUNT

How can I combine that queries in ONE if statement in the SELECT?
Or is it not possible and I need to work with three temp_tables?

Thanks!

Options: ReplyQuote


Subject
Written By
Posted
IF in query
November 10, 2014 04:49AM
November 10, 2014 12:50PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.