MySQL UPDATE - 选择性更新
首先,对这个糟糕的标题表示歉意,我想不出更好的方式来阐明我的问题。 (随意建议更好的替代方案)
基本上我有一个带有“计数”列的表。 我想将除具有最高值的 10 行之外的所有计数重置为零。我希望将它们重置为 0。
如何在不编写多个查询的情况下实现此目的?
更新 我的查询如下,现在
UPDATE covers AS t1
LEFT JOIN (SELECT t.cover_id
FROM covers t
ORDER BY t.cover_views DESC
LIMIT 10) AS t2 ON t2.id = t.id
SET cover_views = 0
WHERE t2.id IS NULL
收到错误 #1054 - “where 子句”中的未知列“t2.id”
- 知道为什么吗?
我也尝试了以下方法,结果相同
UPDATE covers t1
LEFT JOIN (SELECT t.cover_id
FROM covers t
ORDER BY t.cover_views DESC
LIMIT 10) t2 ON t2.id = t.id
SET t1.cover_views = 0
WHERE t2.id IS NULL
First up, apologies for the awful title I couldn't think of a better way to articulate my issue. (Feel free to suggest better altnernatives)
Basically I have a table with a "count" column.
I want to reset all counts to zero except for the 10 rows with the top values. I want them to be reset to 0.
How do I achieve this without writing multiple queries?
Update
I have my query as the following now
UPDATE covers AS t1
LEFT JOIN (SELECT t.cover_id
FROM covers t
ORDER BY t.cover_views DESC
LIMIT 10) AS t2 ON t2.id = t.id
SET cover_views = 0
WHERE t2.id IS NULL
I get the error #1054 - Unknown column 't2.id' in 'where clause'
- any idea why?
I also tried the following with the same result
UPDATE covers t1
LEFT JOIN (SELECT t.cover_id
FROM covers t
ORDER BY t.cover_views DESC
LIMIT 10) t2 ON t2.id = t.id
SET t1.cover_views = 0
WHERE t2.id IS NULL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用:
Use:
尝试:
try:
您可以使用子查询:
You can use a subquery: