2016-04-08 24 views
0

Bir tablodaki tüm satırları, nerede bir yan tümce ile aynı değere sahip satır sayısıyla güncellemek istiyorum.mysql güncelleştirmesi. count, sütun null ve sütunlar aynı olduğu satırlar

SQL Bulunduğum sorgu bu

UPDATE database.TableName AS A 
SET refCount = (SELECT Count(*) 
FROM database.TableName AS B 
WHERE A.file1 = B.file1 AND A.file2 is null)  

TableName benziyor

//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 

|ID | file1  | file2 | refCount | 
| 1 | file.txt |   | 1  | 
| 2 | file.txt |   | 1  | 
| 3 |   | file2.txt | 1  | 
| 4 | file3.txt |   | 1  | 

TableName (Beklenen Sonuçlar) sizin gibi

|ID | file1  | file2 | refCount | 
| 1 | file.txt |   | 2  | 
| 2 | file.txt |   | 2  | 
| 3 |   | file2.txt | 1  | 
| 4 | file3.txt |   | 1  | 

cevap

1

Bak Güncellemenizin ifadesi için nereye maddesini eksik.

bu deneyin

...

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