Jenny
Jenny

Reputation: 1735

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:

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  | user_id  | grade

The best is to make the two step in one-- get IDs and update grade in one code.

Upvotes: 0

Views: 240

Answers (3)

sel
sel

Reputation: 4957

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

Upvotes: 2

Prince Jea
Prince Jea

Reputation: 5690

You can user 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

Upvotes: 2

xdazz
xdazz

Reputation: 160943

Update grades SET grade = ? 
WHERE ID IN (SELECT ID FROM posts WHERE post_parent = ?)

Upvotes: 0

Related Questions