我正在尝试使用连接更新表格。任务是: 修改数据库以显示Helen Partou现在已经学会充分播放手鼓。
这是我的尝试:
update MusicianInstrument
set instrumentName = 'Tambourine',levelOfExpertise = 'Adequate'
from MusicianInstrument join Musician
on MusicianInstrument.musicianNo = Musician.musicianNo
where musicianName = 'Helen Partou';
但是我一直在使用FROM语句错误.....任何帮助?!
谢谢
答案 0 :(得分:2)
您想使用WHERE EXISTS
:
UPDATE MusicianInstrument mi
SET mi.instrumentName = 'Tambourine'
, mi.levelOfExpertise = 'Adequate'
WHERE EXISTS ( SELECT 1 FROM Musician m
WHERE m.musicianNo = mi.musicianNo
AND m.musicianName = 'Helen Partou');
在您注意使用Oracle之后,我建议不要使用CamelCase或无头CamelCase作为对象名称。
答案 1 :(得分:2)
您的任务是修改数据库,因此不仅可能更新表,还可以插入值(如果它们尚未存在)。 您可以使用merge:
执行此操作merge into MusicianInstrument i
using (select * from Musician where musicianName = 'Helen Partou') m
on (m.musicianNo = i.musicianNo
and i.instrumentName = 'Tambourine')
when matched then update set levelOfExpertise = 'Adequate'
when not matched then insert (i.musicianNo, i.instrumentName, i.levelOfExpertise)
values (m.musicianNo, 'Tambourine', 'Adequate')