我应该使用插入选择还是仅使用插入循环?

发布于 2024-08-01 15:59:40 字数 883 浏览 4 评论 0原文

我使用以下答案提取了用户名列表:

如果我的查询中有太多 OR 运算符,如何优化 mysql 查询?

现在我有另一个问题。 我有一个仅由两列组成的朋友表,请求者和好友都形成复合主键。

现在假设我有一个给定用户的 Facebook 好友数组,使用上面的问题。

我如何在我的网络应用程序中自动使他们成为朋友?

这意味着如果我有 5 个名为“usera”的实际朋友。 我需要执行 10 条插入语句。

例如:friendA,usera 和usera,friendA,以便friendA 反映为usera 的好友。

我知道插入选择是有效的。 但不幸的是,这些朋友的用户名列表是通过使用 IN 子句的 PHP 值数组获得的,正如我上面问题中的 SO 成员所建议的那样。

所以我该怎么做?

我应该

a) 循环遍历名称并为每个步骤执行 2 个插入语句吗?

b) 使用插入选择? 但具体该怎么做呢?

我的数据库是这样设计的:

1 个名为 users 的表,其中包含用户名和 fb_uid,其中用户名是主键,fb_uid 可为空。

一张名为“朋友”的表,其中一列名为“请求者”,另一列名为“伙伴”。 两列形成一个复合键。

当朋友表中有以下数据时,我的网络应用程序中的 2 个人(usera,userb)之间就会建立友谊:(usera,userb)和(userb,usera)。

谢谢。

i have extracted a list of usernames using the answers from this:

how to optimize mysql query if i have too many OR operators in my query?

now i have another issue. I have a friend table made up of just 2 columns, requestor and buddy both form a composite primary key.

Now suppose i have an array of facebook friends for a given user using the above SO question.

how do i automatically make them friends in my web app as well?

That means if i have 5 actual friends for a user called 'usera'. i need to perform 10 insert statements.

eg: friendA, usera and usera, friendA so that friendA is reflected as a friend for usera.

i understand that insert select is efficient. but unfortunately the list of usernames of these friends are obtained via a PHP array of values using IN clause as suggested by fellow SO members in my above question.

So what should i do?

should i

a) loop through the names and perform 2 insert statements for each step?

b) use insert select? but how to do it exactly?

my DB is designed this way:

1 table called users with usernames and fb_uid where the username is primary key and fb_uid is nullable.

1 table called friends with a column called requestor and another column called buddy. Both columns form a composite key.

A friendship is established between 2 people (usera, userb) in my web app when you have the following data: (usera, userb) and (userb, usera) in the friends table.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的韩爷 2024-08-08 15:59:40

您始终可以批量插入。

INSERT INTO friends (user_id, friend_user_id) VALUES (1, 2), (2, 1)

You can always make bulk insert.

INSERT INTO friends (user_id, friend_user_id) VALUES (1, 2), (2, 1)
为你拒绝所有暧昧 2024-08-08 15:59:40

我会选择 INSERT SELECT,你可以在这里找到 详细语法

I would go with INSERT SELECT's, you can find the detailed syntax here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文