选择具有最高优先级的行

时间:2016-02-05 11:13:13

标签: mysql

我正在处理查询,而且我有点卡住了。

这是我的疑问:

SELECT *
FROM 
    routine_actions AS ra
JOIN 
    routines AS r ON r.id = ra.routine_id
JOIN 
    account_routines AS ar ON ar.routine_id = r.id
JOIN 
    accounts AS a ON a.id = ar.account_id
WHERE
    (ra.last_run + INTERVAL ra.interval_minutes MINUTE <= NOW() OR ra.last_run IS NULL)
AND
    r.created_at + INTERVAL r.runtime_days DAY > NOW()

我想做的事情:

account有许多例程。一次只能使用一个routine,而routine具有最高优先级。包含优先级列的表为account_routines,因为帐户可以重用例程并指定不同的优先级。

较高的数字等于较高的优先级。目前,该查询正在从所有帐户中提取所有例程。但我只需要一个具有每个帐户最高优先级的例程。

这怎么可能?我不需要解决方案,只是在哪里看,所以我可以弄清楚如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

我认为您需要找到max(优先级)并在连接中包含它以限制返回的行。 e.g。

13
["{=rankedArea?metricType=3902&area={parent-area-identifier}:AdministrativeWard}",
 "{=rankedArea?metricType=3902&area={parent-area-identifier}:{ward-type-identifier}}",
 "{district-short-label}",
 "{child-area-short-label}",
 "{authority-area-short-label}",
 "{=compare?metricType=3343&greater=greater than&equal=equal to&less=less than}",
 "{=countAreas?area={ancestor-2-identifier}:{ancestor-1-type-identifier}}",
 "{=equivalent?metricDimension=[218][218_Number][Specificethnicity][Ethnicity_AsianorAsianBritish]}",
 "{=metricTypeMetadata?metricType=3341&returnValue=source}", "{=value?metricType=3284}",
 "{=percent?metricType=518}",
 "{=rank?metricType=3287}",
 "{=rankedArea?metricType=3286}"]