The problem with your query is that no single row can have both users.
You can move the logic to a having
clause (with a group by
) to do what you want:
SELECT thread_id
FROM post
group by thread_id
having max(user_id = '1') > 0 and
max(user_id ='31') > 0;