我需要根据创建的时间获取最大优先级值。我的问题在这里,
select
t.id, ocf1.Content Priority, ocf1.created
from
Tickets t inner Join ObjectCustomFieldValues ocf1
on ocf1.ObjectId=t.id
where
ocf1.CustomField=301
and t.queue in (27,50,49,52)
and t.status not in ('rejected','deleted')
and t.created between subdate(now(),interval 1500 minute) and now();
id Priority created
-------------------------------
27344 P4 28/01/13 09:17
27344 Incident 28/01/13 09:18
27345 P4 28/01/13 09:22
27346 P4 28/01/13 09:25
27353 P4 28/01/13 10:08
27353 Incident 28/01/13 10:09
27360 P4 28/01/13 10:38
27360 Incident 28/01/13 10:39
27365 P3 28/01/13 11:52
27376 P4 28/01/13 14:49
示例:我需要ID Incident
的最大优先级值27344
。与其他ID相同。
答案 0 :(得分:3)
SELECT a.*, b.* -- select your desired columns here
FROM Tickets a
INNER JOIN ObjectCustomFieldValues b
ON b.ObjectId = a.id
INNER JOIN
(
SELECT ObjectId, MAX(created) max_val
FROM ObjectCustomFieldValues
WHERE CustomField = 301
GROUP BY ObjectId
) c ON b.ObjectId = c.ObjectId AND
b.created = c.max_val
WHERE b.CustomField = 301 AND
a.queue IN (27,50,49,52) AND
a.status NOT IN ('rejected','deleted') AND
a.created BETWEEN SUBDATE(NOW(), INTERVAL 1500 MINUTE) AND NOW()
您的日期是否真的格式如下:28/01/13 09:17
?而数据类型是字符串。对?如果是这种情况,您需要将其转换为日期时间,以便MAX()
正常运行。