2

我正在尝试为帐户中的每个用户从活动表中选择最新的 RowID。但是每次我运行查询时,它都会返回正确的 RowID,但其他信息似乎是随机选择的,因为主题和日期总是来自较早的 RowID。

为什么 MySQL 选择正确的最新 RowID,然后返回 SubjectDate 等的随机值。

`

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;

任何人都看到我可能做错了什么?我曾尝试按活动使用分组。ContactID,活动。SMSReceived,但仍然没有乐趣。

谢谢

4

1 回答 1

1

尝试这样的事情:

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;

这里的想法是所需的信息与用户有关,因此 GROUP BY 需要针对用户。GROUP_CONCAT 将只挑选出符合 IF 条件的分组行中的那一行。

- 只是一个想法

于 2013-12-21T04:14:13.170 回答