I run this query to GROUP BY session_id and get the latest (time) records:
$query = "SELECT type, session_id, session_name, session_email, time
FROM chat
WHERE time IN (
SELECT MAX(time)
FROM chat
GROUP BY session_id
)
ORDER BY time DESC";
Yet, I only want the latest records for type and time. How can I still get the first records for session_id, session_name, session_email?
$query = " SELECT type, session_id, session_name, session_email, time FROM chat WHERE time IN ( SELECT MAX(time) FROM chat GROUP BY session_id ) ORDER BY time DESC LIMIT 5 /* gives you the first 5 records UNION SELECT type, session_id, session_name, session_email, time FROM chat WHERE time IN ( SELECT MAX(time) FROM chat GROUP BY session_id ) ORDER BY time ASC LIMIT 5 /* gives you the first 5 records in other sequence ";