1

I successfully managed to retrieve users with 2 or less posts from a database and display the result of that query which looks like this:

SELECT Users.*, count( Posts.user_id ) 
FROM Posts LEFT JOIN Users ON Users.user_id = Posts.user_id
GROUP BY Posts.user_id
HAVING count( Posts.user_id ) < 2

What i would like to achieve next is to update a field called active for all users of that query, so i tried to add an UPDATE statement together with the first query:

UPDATE Users
SET Users.Active = 3
WHERE count( Posts.user_id ) < 2

But i can't get it to work. Where should the UPDATE part be positioned or maybe should it be run separately ?

1 Answer 1

1

In where statement you need to select count of posts per user and check if it is less than 2.

    UPDATE Users
    SET Users.Active = 3
    WHERE (SELECT count( Posts.user_id ) 
           FROM Posts 
           WHERE Users.user_id = Posts.user_id
           GROUP BY Posts.user_id) < 2
Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a breeze, thanks a lot for the fast answer Dusan!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.