How to join two table to update one in Postgresql

Viewed 27

I have two table user_table and subscription_table

user_table

| user_id(int)| email(varchar) | phone(int)|
------------------------------------------------
|    1       | abc@gmail.com   |   9910256256 |
|    2       | def@gmail.com   |   8856956325 |
|    3       | ghi@gmail.com   |   8745692455 |
|    4       | jkl@gmail.com   |   7852369526 |

subscription_table

    | email(varchar)            | type(varchar)| is_subscribed(boolean)
    ------------------------------------------------------------------
    |  abc@gmail.com            |   news       | true
    |  abc@gmail.com            |   video      | false
    |  def@gmail.com            |   news       | true
    |  def@gmail.com            |   video      | true

I have user_id and want to update is_subscription. Best wayto update is_subscription.

I have tried to do it with multiple query but i think that is not good.

1 Answers

Is this what you want?

update subscription_table s
    set is_subscription = ?
    from user_table u
    where u.email = s.email and u.user_id= ?;

Note: If you have a user_id, then you should be using that for foreign key relationships. I don't understand why email would be used for this purpose.

Related