2012-08-15 88 views
1

我有幾個連接的查詢。 我的查詢根據他們的總步驟返回排行榜前十位的人。 我想排除privacy列中設置爲out的值中的所有行。排除某一行

MySQL查詢,因爲它代表:

SELECT ga.owner_id, u.displayname, g.title, SUM(ga.steps) as `Total Steps`, u.user_id, g.group_id, pri.privacy FROM engine4_passport_goalactivitys ga 
LEFT JOIN engine4_passport_goals goals ON goals.goal_id = ga.owner_id 
LEFT JOIN engine4_passport_passports p ON p.passport_id = goals.owner_id 
LEFT JOIN engine4_users u ON u.user_id = p.owner_id 
LEFT JOIN engine4_passport_teams t ON t.owner_id = u.user_id 
LEFT JOIN engine4_group_groups g ON g.group_id = t.group_id 
LEFT JOIN engine4_passport_privacy pri on pri.user_id = u.user_id 
GROUP BY u.user_id 
ORDER BY `Total Steps` DESC 
LIMIT 0,10 

我曾嘗試:

SELECT ga.owner_id, u.displayname, g.title, SUM(ga.steps) as `Total Steps`, u.user_id, g.group_id, pri.privacy FROM engine4_passport_goalactivitys ga 
LEFT JOIN engine4_passport_goals goals ON goals.goal_id = ga.owner_id 
LEFT JOIN engine4_passport_passports p ON p.passport_id = goals.owner_id 
LEFT JOIN engine4_users u ON u.user_id = p.owner_id 
LEFT JOIN engine4_passport_teams t ON t.owner_id = u.user_id 
LEFT JOIN engine4_group_groups g ON g.group_id = t.group_id 
LEFT JOIN engine4_passport_privacy pri on pri.user_id = u.user_id 
AND pri.privacy <> 'out' 
GROUP BY u.user_id 
ORDER BY `Total Steps` DESC 
LIMIT 0,10 

上述兩個查詢之間的區別是行:

AND pri.privacy <> 'out'

然而,而不是將隱私設置爲「out」的用戶排除在外em在查詢中,只是將其隱私值設置爲null

我也試過where而不是and,但它只是返回0行。使用它

回答

0

嘗試WHERE子句,而不是在與

where pri.privacy <> 'out' 
+0

我已經試過這一點。它返回0行 – RSM 2012-08-15 10:18:13

0

解決 -

SELECT ga.owner_id, u.displayname, g.title, SUM(ga.steps) as `Total Steps`, u.user_id, g.group_id, pri.privacy FROM engine4_passport_goalactivitys ga 
LEFT JOIN engine4_passport_goals goals ON goals.goal_id = ga.owner_id 
LEFT JOIN engine4_passport_passports p ON p.passport_id = goals.owner_id 
LEFT JOIN engine4_users u ON u.user_id = p.owner_id 
LEFT JOIN engine4_passport_teams t ON t.owner_id = u.user_id 
LEFT JOIN engine4_group_groups g ON g.group_id = t.group_id 
LEFT JOIN engine4_passport_privacy pri on pri.user_id = u.user_id 
WHERE pri.privacy is null OR pri.privacy = 'in' 
GROUP BY u.user_id 
ORDER BY `Total Steps` DESC 
LIMIT 0,10;