簡體   English   中英

mysql更新。 計數,其中column為null且column相同的行

[英]mysql update. count, rows where column is null and a columns is the same

我想用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     |

看起來您缺少更新語句的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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM