2012-08-06 58 views
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_relation.user_id, 
    xf_user_external_auth.provider_key 

看來它選擇具有最低的ID,這不是我想要的組。我希望它選擇顯示優先級最高的組。

所以user_group_id應該是具有最高優先顯示該組即MAX(xf_user_group.display_style_priority)

回答

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 
+0

每一組用戶在現在顯示,所以這是行不通的。 – BlackVoid 2012-08-06 23:16:02

+0

基本上你需要做的是,當你爲任何表列選擇MAX()時,你需要確保該表在GROUP BY子句中生效。 – Kalpesh 2012-08-06 23:17:43