首先,为可怕的标题道歉,我想不出更好的方式来表达我的问题。 (随意建议更好的altnernatives)
基本上我有一个带有“count”列的表。 我想将所有计数重置为零,除了具有最高值的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 - Unknown column 't2.id' in 'where clause'
- 知道为什么?
我也尝试了以下相同的结果
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
答案 0 :(得分:2)
使用:
UPDATE TABLE t1
LEFT JOIN (SELECT t.id
FROM TABLE t
ORDER BY t.id DESC
LIMIT 10) t2 ON t2.id = t1.id
SET TABLE.count = 0
WHERE t2.id IS NULL
答案 1 :(得分:1)
尝试:
update <table> t
left outer join
(
select id from <table> order by <counter> desc limit 10
) c on c.id = t.id
set
<counter> = 0
where
c.id is null;
答案 2 :(得分:0)
您可以使用子查询:
update A set count = 0 where A.id not in
(select id from A order by count desc limit 10)