Jump to content

[SOLVED] SELECT QUERY


eightFX

Recommended Posts

Hello everyone,

 

I am looking for a little select query help. What I am looking to do is create a list of events with the most recent date. What I am looking for is this:

 

(2007-04-02) - The progress of the project

(2007-03-29) - Birthday party for Bob

(2007-03-17) - St. Patrick's Day Celebration

 

What I have in the table is this:

 

EventID | EventName | EventDescription | EventDate

 

1 | Project1 | The progress of the project | 2007-03-01

2 | Stpatricks | St. Patrick's Day Celebration | 2007-03-17

3 | Bob| Birthday party for Bob | 2007-03-28

4 | Bob| Birthday party for Bob | 2007-03-29

5 | Bob| Birthday party for Bob | 2007-03-29

6 | Project1 | The progress of the project | 2007-04-01

7 | Project1 | The progress of the project | 2007-04-02

 

What I had for a query that was working until I wanted to incorporate the date is this:

 

$query = SELECT DISTINCT EventName, EventDescription FROM EventTable ORDER BY EventName

 

Current output:

 

1. Birthday party for Bob

2. The progress of the project

3. St. Patrick's Day Celebration

 

 

So as I stated, the above query works for just displaying a list of the event descriptions but now I want to add the most recent date to the list as shown at the top, I tried adding the date to distinct but obviously that didn't work. I just can't seem how to get this query to have both... Any help much appreciated. Thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/45794-solved-select-query/
Share on other sites

The problem is that the event date can be different throughout. Therefore when I do that it shows the event multiple times which I do not want to happen in the list. I wish it were as easy as sorting by date. As for the current date comparison I am not sure that is what I am looking to do either. The more I look at this I am thinking I will need to do two queries but if someone can confirm that and state what I would need the two queries for that would be great.

Link to comment
https://forums.phpfreaks.com/topic/45794-solved-select-query/#findComment-222836
Share on other sites

The problem is that the event date can be different throughout. Therefore when I do that it shows the event multiple times which I do not want to happen in the list. I wish it were as easy as sorting by date. As for the current date comparison I am not sure that is what I am looking to do either. The more I look at this I am thinking I will need to do two queries but if someone can confirm that and state what I would need the two queries for that would be great.

 

use distinct or group them by date or event whatever you key is

Link to comment
https://forums.phpfreaks.com/topic/45794-solved-select-query/#findComment-223031
Share on other sites

If EventDescription is always the same within an EventName group use:

SELECT EventName, EventDescription, MAX(EventDate) AS EventDate
FROM EventTable GROUP BY EventName

 

If not use:

SELECT
FROM EventTable AS et
JOIN (
    SELECT EventName,  MAX(EventDate) AS maxDate
    FROM EventTable GROUP BY EventName
) AS sub ON et.eventName = sub.EventName AND et.EventDate = sub.maxDate

Link to comment
https://forums.phpfreaks.com/topic/45794-solved-select-query/#findComment-223285
Share on other sites

If EventDescription is always the same within an EventName group use:

SELECT EventName, EventDescription, MAX(EventDate) AS EventDate
FROM EventTable GROUP BY EventName

 

If not use:

SELECT
FROM EventTable AS et
JOIN (
    SELECT EventName,  MAX(EventDate) AS maxDate
    FROM EventTable GROUP BY EventName
) AS sub ON et.eventName = sub.EventName AND et.EventDate = sub.maxDate

 

THANKS ARTACUS! That is exactly what I was looking to do! Thank you again! Kudos!

Link to comment
https://forums.phpfreaks.com/topic/45794-solved-select-query/#findComment-225007
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.