Question

I am trying to select the most recent RowID from an activities table for each user in an Account. But everytime i run the query, it returns the correct RowID but the other information seems to be selected at random as the Subject and Date are always from an earlier RowID.

Why does MySQL select the correct most recent RowID but then return random values for the the SubjectDate etc..

`

SELECT 
MAX(activities.rowid) as RowID,
contacts.firstname as First, 
contacts.lastname as Last,
activities.visiondescription as Subject,
smsreceived as Date
FROM activities, contacts 
WHERE activities.contactid=contacts.contactid 
AND activities.accountid=contacts.accountid
AND activities.accountid = 'AAXA-S0BJ7I'
group by activities.RowID;

Anyone see what i might be doing wrong? I have tried to use group by activities.ContactID, activities.SMSReceived and still no joy.

Thanks

No correct solution

OTHER TIPS

Try something like this:

SELECT contacts.contact_id,       
   contacts.firstname as First,
   contacts.lastname  as Last,
   GROUP_CONCAT(DISTINCT IF(activities.rowid = MAX(activities.rowid),visiondescription,NULL))  AS Subject
   GROUP_CONCAT(DISTINCT IF(activities.rowid = MAX(activities.rowid),smsreceived,NULL)) AS `Date`
FROM activities, contacts 
WHERE activities.contactid=contacts.contactid 
AND activities.accountid=contacts.accountid
AND activities.accountid = 'AAXA-S0BJ7I'
GROUP BY contacts.contact_id;

The idea here is that the needed information pertains to the user and so the GROUP BY needs to be against the user. The GROUP_CONCAT will pick out only that row among the grouped rows that meeds the IF condition.

-- just a thought

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top