简体   繁体   English

如何根据另一个表中的值更新列?

[英]How to update a column based on values in another table?

I want to update some vars $grade in grade table if their ID matches the IDs selected in another table: 如果他们的ID与另一个表中选择的ID相匹配,我想更新成绩表中的一些vars $grade

SELECT ID
FROM posts
WHERE post_parent = %d, $parent_id

With the IDs I got from the above query, now, how to update vars in grade column in this grade table? 现在,使用我从上述查询中获得的ID,如何更新此成绩表中“成绩”列中的vars?

ID  | user_id  | grade

The best is to make the two step in one-- get IDs and update grade in one code. 最好的方法是使两个步骤合而为一-在一个代码中获取ID和更新等级。

UPDATE grade AS g
       INNER JOIN post p
               ON g.id = p.id
SET    g.grade = 'your-intended-grade'
WHERE  p.post_parent = $parent_id

You can user INNER JOIN 您可以使用INNER JOIN

try the code below 试试下面的代码

UPDATE a
SET grade ='your value' FROM grade a
INNER JOIN posts b ON a.id = b.id
WHERE b.post_parent = $parent_id
Update grades SET grade = ? 
WHERE ID IN (SELECT ID FROM posts WHERE post_parent = ?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM