我想更新表中的所有行,其中包含与where子句具有相同值的行数。
SQL
//This gives me the rows I want to update
SELECT file1 FROM database.TableName where file2 is null
//From there I want to count all the rows that are the same
SELECT Count(*) FROM database.TableName where file1 = resulted value
//And then I want to update the row
我当前的查询看起来像这样
UPDATE database.TableName AS A
SET refCount = (SELECT Count(*)
FROM database.TableName AS B
WHERE A.file1 = B.file1 AND A.file2 is null)
表名
|ID | file1 | file2 | refCount |
| 1 | file.txt | | 1 |
| 2 | file.txt | | 1 |
| 3 | | file2.txt | 1 |
| 4 | file3.txt | | 1 |
TableName(预期结果)
|ID | file1 | file2 | refCount |
| 1 | file.txt | | 2 |
| 2 | file.txt | | 2 |
| 3 | | file2.txt | 1 |
| 4 | file3.txt | | 1 |
答案 0 :(得分:1)
看起来你缺少更新语句的where子句。
试试这个......
UPDATE database.TableName AS A
SET refCount =
(SELECT Count(*) FROM database.TableName AS B WHERE A.file1 = B.file1 AND A.file2 is null)
where A.file1 = B.file1