使用内部联接更新两个表

时间:2016-09-07 12:35:38

标签: mysql join sql-update

我有两个表,一个是问题,另一个是回答

问题表的字段为

question_id, question, type, answer_id. 

答案表的字段为

answer_id, question_id, comment, rating, doctor_id

现在我想更新答案属于doctor_id所在的问题。为此我试着写这个查询:

update question q 
set q.question = 'dmvvnnv',a.comment = 'covonfvk',a.rating = 5 
INNER JOIN answer a on q.answer_id = a.answer_id 
WHERE a.doctor_id = 8

但它给了我语法错误:

  

1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以获得正确的语法   靠近' INNER JOIN在q.answer_id = a.answer_id WHERE上回答a   a.doctor_id = 8'在第1行

2 个答案:

答案 0 :(得分:2)

对于UPDATE语法不同的MySQL JOINSET部分应位于JOIN

之后

使用以下查询更新条目:

UPDATE question q
INNER JOIN answer a ON a.answer_id = q.answer_id
SET q.question = 'dmvvnnv'
    ,a.comment = 'covonfvk'
    ,a.rating = 5
WHERE a.doctor_id = 8

答案 1 :(得分:1)

使用以下内容:

UPDATE question q INNER JOIN answer a on q.answer_id = a.answer_id 
SET q.question = 'dmvvnnv',a.comment = 'covonfvk',a.rating = 5
WHERE a.doctor_id = 8