|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Compared to some of the inner join and max statments ive looked at trying to find a solution i guess this is pretty simple.
I have 2 tables: TicketControl (TicketID, Subject, UserID, Status) TicketComments (CommentID, TicketID, Comment, CommentDate) I want to display everything from the ticket control file, and then display the date of the most recent comment. I have tried adapting this statement: SELECT ticketcontrol.*,ticketcomments.* FROM ticketcontrol INNER JOIN ticketcomments ON ticketcontrol.ticketid = ticketcomments.ticketid But it returns details on all of the comments for each control record |
|
#2
|
|||
|
|||
|
I've not used INNER JOIN's before, so maybe someone else could shed some light on this, but have you tried something like this:
Code:
SELECT ticketcontrol.*, ticketcomments.* FROM ticketcontrol, ticketcomments WHERE ticketcontrol.TicketID = ticketcomments.TicketID
__________________
http://www.phptutorials.cjb.net. go on, give it a click! |
|
#3
|
||||
|
||||
|
how about something like this?
Code:
SELECT ticketcontrol.ticketid, ticketcontrol.subject,
ticketcontrol.userid, ticketcontrol.status,
ticketcomments.commentdate
FROM TicketControl, TicketComments
WHERE ticketcontrol.ticketid = ticketcomments.ticketid
GROUP BY ticketcomments.commentdate
|
|
#4
|
|||
|
|||
|
Thanks, I think that helped.
OK, so far I have: Code:
SELECT ticketcontrol.*, ticketcomments.commentdate FROM ticketcontrol, ticketcomments WHERE ticketcontrol.ticketid = ticketcomments.ticketid GROUP BY ticketcomments.commentdate Which im sure is an improovement on my first query however I am still getting a record returned for EACH of the comments. I only want the most recent comment for the ticket to be selected. |
|
#5
|
|||
|
|||
|
Order by comment date, then only use the first returned record
|
|
#6
|
|||
|
|||
|
That wouldnt work as I need each record from the control file and only one record from the comment file
|
|
#7
|
|||
|
|||
|
okay in that case you probably want two sql queries, one to get the details from TicketControl, the as you loop through get the latest comment for each record from TicketComments, like:
PHP Code:
Not the nicest code i know but i'm writing it on the fly, hopfully it will give you some idea what i'm talking about. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Simple SQL Query (INNER JOIN + MAX) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|