SQL连接更新另一行有2的行?

时间:2013-06-07 10:58:39

标签: php sql

这就是我的尝试:

UPDATE tbl_questions 
JOIN (SELECT tbl_users.uid, tbl_users.is_spam
FROM tbl_users JOIN tbl_questions ON tbl_users.uid = tbl_questions.uid
)
SET question_type_id = 2
WHERE is_spam = 2;

我想将那些问题question_type_id更新为2,其中用户表is_spam = 2。 我的sql写为错误Every derived table must have its own alias ...

tbl_questions => uid, question_type_id,
tbl_users => uid, is_spam,

2 个答案:

答案 0 :(得分:1)

子查询必须具有别名。

UPDATE tbl_questions 
JOIN 
(
   SELECT tbl_users.uid, tbl_users.is_spam
   FROM tbl_users 
   JOIN tbl_questions ON tbl_users.uid = tbl_questions.uid
) sub_query_alias on sub_query_alias.uid = tbl_questions.uid
SET question_type_id = 2
WHERE is_spam = 2;

我在上面的查询中使用了sub_query_alias。更简单的查询是

UPDATE tbl_questions q
JOIN tbl_users u ON u.uid = q.uid AND u.is_spam = 2
SET q.question_type_id = 2

答案 1 :(得分:0)

请检查: -

更新t1从tbl_questions AS t1设置t1.question_type_id = 2     JOIN tbl_users AS t2 ON t1.tbl_questions.uid = t2.uid WHERE t2.is_spam = 2;