1

I have an events table in my database, and each event can have up to 5 different dates. Date1 in the table is always before date2, date2 before date3 etc.

Given 2 dates in the search form, I'm trying to find the events between them.

My table is designed as follows:

id | name  | date1     | date2      | date3     | date4     | date5
1  | test1 | 2013-05-24| 2013-05-25 | 0000-00-00| 0000-00-00| 0000-00-00
2  | test2 | 2013-06-01| 2013-06-08 | 2013-06-15| 2013-06-23| 2013-06-30
3  | test3 | 2013-03-15| 0000-00-00 | 0000-00-00| 0000-00-00| 0000-00-00

$datefrom , $dateto are the two variables from the search form. Now the $datefrom for example, will always search in date1 of the table. But $dateto has to search in date5, and if it is null to date4 and so on.

The best query I came up with so far is this:

SELECT * FROM events 
WHERE 
IF(date1 != '0000-00-00', IF(date1>='2012-12-19', 1, 0),0) = 1 
AND CASE 
WHEN date5!='0000-00-00' THEN IF(date5<='2012-12-31', 1, 0)     
WHEN date4!='0000-00-00' THEN IF(date4<='2012-12-31', 1, 0)     
WHEN date3!='0000-00-00' THEN IF(date3<='2012-12-31', 1, 0) 
WHEN date2!='0000-00-00' THEN IF(date2<='2012-12-31', 1, 0) 
END 

But it is not working very well, for example this query returns rows with its oldest date being in 2013. I don't know if CASE is the right approach to begin with. Any ideas?? Thanks for your time!

2
  • I don't really understand the concept: you have random dates for each event - why should the data should make sense, when the events don't have any relation to each other. Or are you handling each and every event request separately? Commented Dec 22, 2012 at 15:54
  • Can you show the expected results based on above sample data table? Commented Dec 22, 2012 at 16:03

2 Answers 2

2

you can use it so simple with GREATEST

SELECT name , date1 date_start, GREATEST(date2,date3,date4,date5) date_end
FROM events

and will output like that

 NAME   DATE_START  DATE_END
 jack   2013-05-24  2013-05-25
 peter  2013-06-01  2013-06-30

here THE DEMO SQLFIDDLE

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you thank you thank you! Not so much for the answer, but for exposing me to sqlfiddle.... heard of jsfiddle but not this one... now I can fiddle with sql.... weeeee!
@DudeSolutions nice that it help you this answer :) ,
Thank you so very much for this! I didn't even know about GREATEST. MySQL keeps amazing me!
I'm sorry but I'm new to stackoverflow. How do I choose your answer as the correct one??
nice that it helped you , good luck with the rest of your work :).
0

Try this:

SELECT 
  id, 
  name
FROM
(
    SELECT 
      id, 
      name, 
      MIN(EventDate) AS "FromDate",
      MAX(EventDate) AS "ToDate"
    FROM
    (
      SELECT id, name, date1 As "EventDate" FROM events
      UNION ALL
      SELECT id, name, date2                FROM events
      UNION ALL
      SELECT id, name, date3                FROM events
      UNION ALL
      SELECT id, name, date4                FROM events
      UNION ALL
      SELECT id, name, date5                FROM events
    ) t 
    GROUP BY id, name
) t
WHERE FromDate > @fromdate
  AND ToDate   < @todate;

SQL Fiddle Demo

However, if you want to get the data in the same way as it is in the events table, you can do this:

SELECT 
  e.*
FROM
(
    SELECT 
      id, 
      name, 
      MIN(EventDate) AS "FromDate",
      MAX(EventDate) AS "ToDate"
    FROM
    (
      SELECT id, name, date1 As "EventDate" FROM events
      UNION ALL
      SELECT id, name, date2                FROM events
      UNION ALL
      SELECT id, name, date3                FROM events
      UNION ALL
      SELECT id, name, date4                FROM events
      UNION ALL
      SELECT id, name, date5                FROM events
    ) t 
    GROUP BY id, name
) t
INNER JOIN events e ON t.id = e.id
WHERE t.FromDate > @fromdate
  AND t.ToDate   < @todate;

For instance, for @fromdate = '2012-05-01' and @todate = '2012-06-15', this will give you:

| ID |  NAME |                      DATE1 |                      DATE2 |  DATE3 |  DATE4 |  DATE5 |
---------------------------------------------------------------------------------------------------
|  1 | test1 | May, 24 2013 00:00:00+0000 | May, 25 2013 00:00:00+0000 | (null) | (null) | (null) |

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.