Adding values in column based on another column of separate table sql

melissa

I have two tables

Followers_list

User_name    Followers_name
user 1          user 2
user 1          user 3
user 3          user 4

Final_table

 User_name         M_User     Total    Follower
  user 1           user2       8         NULL
  user 2           user 3      9         NULL
  user 3           user 4      2         NULL

What I want is alter table like this

 User_name         M_User     Total    Follower
  user 1           user2       8         1
  user 2           user 3      9         0
  user 3           user 4      2         1

Basically I want to match the two tables and see if the user_name is following m_user and put 1 if yes else 0 this is what I have done so far but it's giving me "FL"."F_USER_NAME": invalid identifier error.

UPDATE FINAL_TABLE   SET FB.FOLLOWER =  (CASE
        WHEN FB.USER_NAME = FL.USER_NAME AND FB.M_USER = FL.F_USER_NAME 
     THEN       1
        ELSE  0
    END);

    FROM
        FINAL_TABLE fb,
        FOLLOWERS_LIST fl

How can I solve this problem?

JeromeFr

You can use a subquery to retrieve the value you want to assign, like this :

UPDATE Final_table FB
  SET FB.FOLLOWER =
    (SELECT CASE WHEN COUNT(*) > 0 THEN 1
                       ELSE  0 END
        FROM FOLLOWERS_LIST fl
        WHERE FB.USER_NAME = FL.USER_NAME AND FB.M_USER = FL.F_USER_NAME);

Here is my testing : https://livesql.oracle.com/apex/livesql/s/el9abzc95lirn6mrt4l0lfod7

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL Update Column based on separate table conditions

From Dev

SQL Adding a Column to table, based on a inner join

From Dev

Excel - sum values based on a column that match another column in another table

From Dev

Sql column adding another column

From Dev

Sql column adding another column

From Dev

create table column based on another table column value in sql

From Dev

create table column based on another table column value in sql

From Dev

Move SQL Server table column values to another table column

From Dev

Move SQL Server table column values to another table column

From Dev

R Data.table divide values in column based on another column

From Dev

Lexicographical sorting of a Postgres table column based on values of another column

From Dev

Populate a column in SQL with values from another table

From Dev

Update SQL Table based on column values

From Dev

How to find adjacent values in a column based on another column value in Sql

From Dev

Aggregating column based on datetime column of another table PySpark/SQL

From Dev

SQL add count column based on same table and grouped by another column

From Dev

Adding a factor column based on parts of another column

From Dev

How to filter an Excel table based on values in a column shard with another table?

From Dev

Set two values in one table based on sum of a column in another table

From Dev

Updating column values in a table based on join with another table?

From Dev

Adding all the values of a column if another column matches

From Dev

Adding all the values of the column based on column Header

From Dev

Adding column data based on column values?

From Dev

Adding another column based on different criteria (SQL-server)

From Dev

Adding another column based on different criteria (SQL-server)

From Dev

Update a column in a table based on column in another table

From Dev

Insert record in a table based on a column in another table (in Oracle SQL developer)

From Dev

SQL Pivot - adding another column

From Dev

SQLite - Update a column based on values from another table's columns

Related Related

  1. 1

    SQL Update Column based on separate table conditions

  2. 2

    SQL Adding a Column to table, based on a inner join

  3. 3

    Excel - sum values based on a column that match another column in another table

  4. 4

    Sql column adding another column

  5. 5

    Sql column adding another column

  6. 6

    create table column based on another table column value in sql

  7. 7

    create table column based on another table column value in sql

  8. 8

    Move SQL Server table column values to another table column

  9. 9

    Move SQL Server table column values to another table column

  10. 10

    R Data.table divide values in column based on another column

  11. 11

    Lexicographical sorting of a Postgres table column based on values of another column

  12. 12

    Populate a column in SQL with values from another table

  13. 13

    Update SQL Table based on column values

  14. 14

    How to find adjacent values in a column based on another column value in Sql

  15. 15

    Aggregating column based on datetime column of another table PySpark/SQL

  16. 16

    SQL add count column based on same table and grouped by another column

  17. 17

    Adding a factor column based on parts of another column

  18. 18

    How to filter an Excel table based on values in a column shard with another table?

  19. 19

    Set two values in one table based on sum of a column in another table

  20. 20

    Updating column values in a table based on join with another table?

  21. 21

    Adding all the values of a column if another column matches

  22. 22

    Adding all the values of the column based on column Header

  23. 23

    Adding column data based on column values?

  24. 24

    Adding another column based on different criteria (SQL-server)

  25. 25

    Adding another column based on different criteria (SQL-server)

  26. 26

    Update a column in a table based on column in another table

  27. 27

    Insert record in a table based on a column in another table (in Oracle SQL developer)

  28. 28

    SQL Pivot - adding another column

  29. 29

    SQLite - Update a column based on values from another table's columns

HotTag

Archive