简体   繁体   中英

mySQL, three tables: UPDATE multiple rows (each with a different value)

I have three tables in mySQL:

TABLE:CollectionAttributeValues
cID
akID
avID

TABLE: atDefault
avID
value

TABLE: CollectionVersions
cID
cvName

Looks Like

CollectionVersions
cID  cvName
1    Billete
5    Tony

atDefault
avID value
1    B.B
3    T.T

CollectionAttributeValues
cID akID avID
1   29   1
5   29   3

I need to take all the values (the column named values) in atDefault" and put it into cvName in CollectionVersions WHERE akID = 29 in CollectionAttributeValues

Basically, take "Billette" and change it to "BB". AND also take "Tony" and change it to "TT".

So far I came up with this

SELECT value
FROM `atDefault` AS d
LEFT JOIN `CollectionAttributeValues` AS v ON d.avID = v.avID
WHERE v.akID =29

But I don't know how to insert the resulting values into the "cvName" column in CollectionVersions...

To UPDATE all the columns of the table CollectionVersions with the data that you get form the query. Try the below query -

UPDATE CollectionVersions cv 
SET cvName = 
(SELECT value
 FROM `atDefault` AS d
 LEFT JOIN `CollectionAttributeValues` AS v ON d.avID = v.avID
 WHERE v.akID =29
 AND cv.cID = v.cID)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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