compudocs Posted June 6, 2009 Share Posted June 6, 2009 I am trying to query info from two tables and filter the query on the MAX date on one of field within the query. I need to do this with a query because I need to export this query via function(exportcsv). Here is what I have: $query_Recordset1 = "SELECT Customer.beenawhile, Customer.CID, MAX(Ticket.ID) as ID, MAX(Ticket.CDate) as MCDate FROM Customer INNER JOIN Ticket ON Customer.CID = Ticket.CID WHERE Email IS NULL AND MCDate < $sixmonthsago Group By CID Order by Ticket.CDate"; I would like to be able to add "AND MCDate < $sixmonthsago " in the WHERE but it doesn't like that. The MCDate is a MAX(Ticket.CDate) within the query(see query). I have shortened up the query for readablility. Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Group By CID Order by Ticket.CDate' at line 1 Does anyone know how to do this? Function, array??? Thanks for any help Steve Link to comment https://forums.phpfreaks.com/topic/161192-solved-filter-on-max-in-query/ Share on other sites More sharing options...
compudocs Posted June 6, 2009 Author Share Posted June 6, 2009 MySQL server version = 5.0 Link to comment https://forums.phpfreaks.com/topic/161192-solved-filter-on-max-in-query/#findComment-850591 Share on other sites More sharing options...
Ken2k7 Posted June 6, 2009 Share Posted June 6, 2009 SELECT c.beenawhile, c.CID, MAX(t.ID), MAX(t.CDate) AS MCDate FROM Customer c INNER JOIN Ticket t ON c.CID = t.CID WHERE c.Email IS NULL HAVING MCDate < $something GROUP BY c.CID ORDER BY t.CDate Link to comment https://forums.phpfreaks.com/topic/161192-solved-filter-on-max-in-query/#findComment-850639 Share on other sites More sharing options...
compudocs Posted June 8, 2009 Author Share Posted June 8, 2009 After a little tinkering, got this to work. Thanks You!!!! SELECT c.beenawhile, c.CID, MAX(t.ID), MAX(t.CDate) AS MCDate FROM Customer c INNER JOIN Ticket t ON c.CID = t.CID WHERE c.Email IS NULL GROUP BY c.CID HAVING MCDate < $something ORDER BY t.CDate Link to comment https://forums.phpfreaks.com/topic/161192-solved-filter-on-max-in-query/#findComment-851593 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.