简体   繁体   中英

MYSQL - insert into table or update if anything is different

Okey, so im trying to store the logged in facebook user´s friends list into a MYSQL table.

Im storing the logged in user data: ID and Name; And am storing the logged in user friends list: ID and Name;

So the table looks like this:

['facebook_user_id']  ['facebook_user_name'] ['facebook_friend_id']  ['facebook_friend_name']
123123123123            User1                  23424234234234          User 20
231321231231            User2                  23424234234234          User 20

So you can see that user1 and user2 has the same friend and if user friend 'User 20' change his/her name I need to update the information, but only for the logged in user, so if user1 logs in the effect will only effect him/her.

But if user1 gets a new friend I need to insert the new friend and update if theres any changes.

How should I do this with PHP? I have seen something similar but didnt work that well for me.

    INSERT INTO table (facebook_user_id, facebook_user_name, facebook_friend_id, facebook_friend_name) 
VALUES ('facebook_user_id' (to short it down) .....)

ON DUPLICATE KEY UPDATE (facebook_user_id=facebook_user_id (and for every entry)....)

Thanks in advance, and any pointers will help me out alot.

I think this is what you meant. I am trying to save user A Facebook friends into a table. When someone logs in with Facebook I am storing, their name and Facebook id. As well as their friends, Facebook id and name.

This is how I would do it. Here is my solution if you need generated schema code let me know.

- Create three tables: registered users, buddies, lookup table

table 1: registered users

[id, userId, facebookUid]

table 2: buddies

[id, buddyid, buddy name]

table 3: You need a lookup table.

[id, userId, buddyid]

Steps:

  1. When UserA logs in with Facebook, save their buddies into the buddies table
  2. This approach above you don't have to worry about duplicate names. If someone updates their name, it will be changed across the board.
  3. You can use join to find who has buddies in common.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM