选择具有最高显示优先级的组

时间:2012-08-06 22:36:06

标签: mysql

我在选择具有最高显示优先级的组时遇到问题。 查询是:

SELECT
    xf_user_group_relation.user_id, xf_user_field_value.field_value as custom_flags,
    xf_user_external_auth.provider_key,
    xf_user_group_relation.user_group_id,
    MAX( xf_user_group.display_style_priority ) AS display_style_priority,
    xf_user_group_relation.user_group_id IN(5,6,11,12) AS is_vip
FROM xf_user_group_relation
INNER JOIN xf_user_group
    ON xf_user_group_relation.user_group_id = xf_user_group.user_group_id
INNER JOIN xf_user_external_auth
    ON xf_user_group_relation.user_id = xf_user_external_auth.user_id
LEFT JOIN xf_user_field_value
    ON xf_user_group_relation.user_id = xf_user_field_value.user_id
    AND xf_user_field_value.field_id = 'gtarp_custom_flags'
WHERE
    xf_user_external_auth.provider = 'steam'
    AND xf_user_group.display_style_priority >= 1000
    AND xf_user_group_relation.user_group_id NOT IN(19,20)
GROUP BY
    xf_user_group_relation.user_id,
    xf_user_external_auth.provider_key

它似乎选择了id最低的组,这不是我想要的。我希望它选择具有最高显示优先级的组。

因此user_group_id应该是具有最高显示优先级的组,即MAX( xf_user_group.display_style_priority)

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT
    xf_user_group_relation.user_id, xf_user_field_value.field_value as custom_flags,
    xf_user_external_auth.provider_key,
    xf_user_group_relation.user_group_id,
    MAX( xf_user_group.display_style_priority ) AS display_style_priority,
    xf_user_group_relation.user_group_id IN(5,6,11,12) AS is_vip
FROM xf_user_group_relation
INNER JOIN xf_user_group
    ON xf_user_group_relation.user_group_id = xf_user_group.user_group_id
INNER JOIN xf_user_external_auth
    ON xf_user_group_relation.user_id = xf_user_external_auth.user_id
LEFT JOIN xf_user_field_value
    ON xf_user_group_relation.user_id = xf_user_field_value.user_id
    AND xf_user_field_value.field_id = 'gtarp_custom_flags'
WHERE
    xf_user_external_auth.provider = 'steam'
    AND xf_user_group.display_style_priority >= 1000
    AND xf_user_group_relation.user_group_id NOT IN(19,20)
GROUP BY
    xf_user_group.display_style_priority,
    xf_user_group_relation.user_id,
    xf_user_external_auth.provider_key