MySQl:如何排除排名子查询中的行
我有一个 mySQL 语句,它选择一些数据并进行排名。
例如,我想要“Bob”的记录被选中但不包含在排名中。因此,我需要在主 select 语句中返回 Bob 的行,但我需要将 Bob 从处理排名的子 SELECT 中排除。我需要鲍勃的数据,但是,他不应该被计入排名。
我在 WHERE x.category = t.category
之后尝试了 AND t.name !='Bob'
但是,这不起作用。
SELECT t.name,
t.category,
t.score1,
t.score2,
(SELECT COUNT(*)
FROM my_table x
WHERE x.category = t.category
AND (x.score1 + x.score2) >= (t.score1 + t.score2)
) AS rank
FROM my_table t ORDER BY rank ASC
有什么建议吗?
谢谢。
-拉克斯米迪
I've got a mySQL statement that selects some data and also ranks.
I want to have the record for 'Bob', for example, selected but NOT included in the ranking. So, I need Bob's row returned in the main select statement, but I need Bob excluded from the sub-SELECT which handles the ranking. I needs Bob's data but, he should not be counted in the rankings.
I tried AND t.name !='Bob'
after WHERE x.category = t.category
But, that's not working.
SELECT t.name,
t.category,
t.score1,
t.score2,
(SELECT COUNT(*)
FROM my_table x
WHERE x.category = t.category
AND (x.score1 + x.score2) >= (t.score1 + t.score2)
) AS rank
FROM my_table t ORDER BY rank ASC
Any suggestions?
Thank you.
-Laxmidi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能应该使用
AND x.name != 'Bob'
而不是t.name
。You should probably use
AND x.name != 'Bob'
instead oft.name
.