排除某一行

时间:2012-08-15 10:14:54

标签: mysql select

我有几个连接的查询。 我的查询根据总步数返回排行榜的前十名。 我想排除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”的用户排除在内,而是将其包含在查询中,并将其隐私值设置为null

我也尝试了where而不是and,但它只返回0行。

2 个答案:

答案 0 :(得分:0)

尝试在WHERE子句中使用它而不是AND

where pri.privacy <> 'out'

答案 1 :(得分: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;