简体   繁体   English

MYSQL - 插入表或更新,如果有什么不同

[英]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. Okey,所以我试图将登录的facebook用户好友列表存储到MYSQL表中。

Im storing the logged in user data: ID and Name; 我存储登录的用户数据:ID和名称; And am storing the logged in user friends list: ID and Name; 并存储登录的用户好友列表:ID和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. 所以你可以看到user1和user2有相同的朋友,如果用户朋友'User 20'更改了他/她的名字,我需要更新信息,但仅限登录用户,所以如果user1登录效果只会影响他她。

But if user1 gets a new friend I need to insert the new friend and update if theres any changes. 但是如果user1得到一个新朋友,我需要插入新朋友并更新,如果有任何变化。

How should I do this with PHP? 我应该如何用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. 我正在尝试将用户A Facebook朋友保存到表格中。 When someone logs in with Facebook I am storing, their name and Facebook id. 当有人使用Facebook登录时,我正在存储,他们的名字和Facebook ID。 As well as their friends, Facebook id and name. 以及他们的朋友,Facebook的ID和名字。

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 表1:注册用户

[id, userId, facebookUid]

table 2: buddies 表2:伙计们

[id, buddyid, buddy name]

table 3: You need a lookup table. 表3:您需要一个查找表。

[id, userId, buddyid]

Steps: 脚步:

  1. When UserA logs in with Facebook, save their buddies into the buddies table UserA使用Facebook登录时,将他们的好友保存到buddies表中
  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. 您可以使用join来查找谁有共同的好友。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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