If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Help with confusing SQL statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-16-11, 13:21
snoop168 snoop168 is offline
Registered User
 
Join Date: Sep 2011
Posts: 5
Help with confusing SQL statement

Hi all, I hope someone could help me out with this sql select statement. I will try my best to describe it.

My current sql statement is:
SELECT *
FROM road_status rs, roadclosure rc, status, problem, sector,
(SELECT max(statustime) as maxtime, roadclosureid
FROM road_status
GROUP BY roadclosureid) maxresults
WHERE rc.roadclosureid = maxresults.roadclosureid
AND rs.statustime= maxresults.maxtime
AND rc.problemid = problem.problemid
AND rc.sectorid = sector.sectorid
AND rs.roadclosureid = rc.roadclosureid
AND rs.statusid = status.statusid
AND status.statusid <> 2

That all works fine above and just to explain that "nested" select statement... Basically have a table that keeps a record of all the status changes and I always want the latest status to return with the roadclosure... So that all works...

i have another table called "notification" with the folloing fields:
"notificationid" (primary, AI)
"roadclosureid" (FK points back to "roadclosure.roadclosureid")
"agencyid"
"notificationtime"

And another table "agency" with fields:
"agencyid" (primary, AI)
"agencyname"

What I want to return with the same select query above is the agencyname for the latest "notificationtime" BUT... If there is no notification it should still pull them up just as null values. So what I think I need is somehow adding in a: LEFT JOIN notification ON notification.roadclosureid = rc.roadclosureid and then also link in the agency table so the actual "agencyname" is returned instead of just its ID... I also think I would need another one of those nested selects in order to find the latest "notificationtime" if one exists and if not again, i still just want null values returned and my PHP will deal with that accordingly.

I have tried a few ways. I havent kept track of them but one way would not return any results if there were no records in the "notification" table probably because i just did a regular join and also if there were multiple "notification" records for the same roadclosureid then it would return multiple lines for each one basically because it wasn't limiting it to the max(notificationtime) maybe because i was missing a WHERE statement

I know I could probably just write out a second select statement to get the information I am looking for but i would prefer to have it all come back in the one resultset so I don't have to worry about dealing with 2 separate array's worth of results and keeping them linked together.

Thanks in advance for any help you could give. If you need more information about the other tables let me know...
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On