从嵌套选择创建更新语句

时间:2013-10-26 22:30:10

标签: mysql sql

我在下面有一个mysql语句

select c11 as genre, t3.strGenre, t2.idGenre t2idGenre, t1.idMVideo
from musicvideo t1
join genrelinkmusicvideo t2
on t2.idMVideo=t1.idMVideo
join genre t3
on t3.idGenre=t2.idGenre
where not (c11=strGenre) and genre='Alternative Rock'

这很好,并且给了我想要的结果。

现在,当我想把它放到更新声明中时,我会陷入困境。

update t2
set idGenre=62
where (select c11, t3.strGenre, t2.idGenre t2idGenre
from musicvideo t1
join genrelinkmusicvideo t2
on t2.idMVideo=t1.idMVideo
join genre t3
on t3.idGenre=t2.idGenre
where not (c11=strGenre) and genre='Alternative Rock')

我知道这不对。任何帮助,将不胜感激。

2 个答案:

答案 0 :(得分:2)

尝试:

update genrelinkmusicvideo t2
    join musicvideo t1
       on t2.idMVideo = t1.idMVideo
    join genre t3
       on t3.idGenre = t2.idGenre
set t2.idGenre = 62
where c11 <> strGenre and genre = 'Alternative Rock'

仅供参考,我建议使用table_alias.column(例如t3.genre = 'Alternative Rock'),因为它使事情变得更加清晰,特别是对于那些不了解架构的人。

答案 1 :(得分:0)

你可以尝试:

update genrelinkmusicvideo 
set idGenre=62
where idGenre IN (select  t2.idGenre 
from musicvideo t1
join genrelinkmusicvideo t2
on t2.idMVideo=t1.idMVideo
join genre t3
on t3.idGenre=t2.idGenre
where not (c11=strGenre) and genre='Alternative Rock')