我有三个表:
表1 :(消费者)
UserName | FirstName | LastName
'Magika12' 'Ronald' 'Ludwig'
表2:(ConsumerLocation)
UserName | LocationID
'Magika12' 13234
表3 :(位置)
LocationID | StreetNumber | StreetName | Suburb | City | Postalcode
13234 13 Baker Street Melton Brisbane 4242
我想改为将
Magika12
的地址改为:"124 Braelands Crescent, Albion, Melbourne, 9999"
从而新表将如下所示:
LocationID | StreetNumber | StreetName | Suburb | City | Postal code
13234 124 Braelands Crescent Albion Melbourne 9999
我已经尝试过这样的事情:
UPDATE
L1
SET
L1.StreetNumber = 124,
L1.StreetName = 'Braelands Crescent',
L1.Suburb = 'Albion' ,
L1.City = 'Melbourne',
L1.Postalcode = 9999
FROM Location L1
INNER JOIN ConsumerLocation
WHERE ConsumerLocation.UserName = 'Magika'
我知道这是不正确的,但是我不确定如何将所有表连接在一起以更新
Magika12
的地址。我已经将UserName
设为Consumer
的主键,并将ConsumerLocation
用作主外键。 LocationId
Location
的主键和Consumer Location
的主外键。任何帮助,将不胜感激
最佳答案
UPDATE
Location L1
JOIN ConsumerLocation CL ON CL.LocationID = L1.LocationID
SET
L1.StreetNumber = 124,
L1.StreetName = 'Braelands Crescent',
L1.Suburb = 'Albion' ,
L1.City = 'Melbourne',
L1.Postalcode = 9999
WHERE CL.UserName = 'Magika12';
如果您有多个与同一个用户名相关的位置条目,请添加到WHERE子句中的LocationId:
WHERE CL.UserName = 'Magika12' AND CL.LocationID = 13234
关于mysql - 变更表信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39932220/