简体   繁体   English

MySQL更新具有相同值的多行

[英]mySQL update multiple rows with the same value

I have a files table 我有一个files

(userID, fileID, fileName, folderID, folderName).

i want to update multiple rows for a user where duplicates are found such as 我想为发现重复的用户更新多行,例如

currently, I have userID=1 who has 2 files both under the folderName of 'work' and folderID of '1' , 目前,我有userID=1 ,在'work' folderID of '1'folderName下都有2 files

i want to update the folderName to 'work1' but keep the same folderID of '1' . 我想将folderName更新为'work1'但保持相同的folderID of '1'

this is my current sql statement and i can't get it to work: 这是我当前的sql语句,我无法使其正常工作:

UPDATE files SET fileFolder = '$folderName'  
WHERE folderID = '$folderID' AND userID = '$userID'
UPDATE files SET fileFolder = '$folderName'  
WHERE folderID = '$folderID' AND userID = '$userID'

instead of setting fileFolder, did you try using folderName ? 您是否尝试使用folderName而不是设置fileFolder? such as .. 如 ..

UPDATE files SET folderName= '$folderName'  
WHERE folderID = '$folderID' AND userID = '$userID'

you need to update folder name eg work to work1 if work folder has more than one time. 如果工作文件夹多次,则需要将文件夹名称更新,例如将work更改为work1。 so you need ti check if old folder has more than once. 因此,您需要检查旧文件夹是否有多个。

Try something like this( not tested): 尝试这样的事情(未经测试):

  UPDATE files SET folderName= '$folderName'  
    WHERE folderID = '$folderID' AND userID = '$userID' AND ( 1 < (SELECT count(*) from files where folderID = $folderID and folderName='oldFolderName'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM