bala
bala

Reputation: 125

How to join two table to update one in Postgresql

I have two table user_table and subscription_table

user_table

| user_id(int)| email(varchar) | phone(int)|
------------------------------------------------
|    1       | [email protected]   |   9910256256 |
|    2       | [email protected]   |   8856956325 |
|    3       | [email protected]   |   8745692455 |
|    4       | [email protected]   |   7852369526 |

subscription_table

    | email(varchar)            | type(varchar)| is_subscribed(boolean)
    ------------------------------------------------------------------
    |  [email protected]            |   news       | true
    |  [email protected]            |   video      | false
    |  [email protected]            |   news       | true
    |  [email protected]            |   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.

Upvotes: 2

Views: 32

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270401

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.

Upvotes: 3

Related Questions