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”的用户,而是将它们包含在查询中,并将其隐私值设置为null

我也尝试过whereand但它只返回 0 行。

4

2 回答 2

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;
于 2012-08-15T10:39:25.100 回答
0

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

where pri.privacy <> 'out'
于 2012-08-15T10:17:43.350 回答