php使用explode和一个包含多个值的字符串链接两个表

发布于 2024-11-30 20:04:25 字数 476 浏览 1 评论 0原文

这是我的第一个问题,所以希望它有意义。

当一个表包含具有多个值的字符串时,如何使用 php/mysql 链接两个表。

基本上,一个表是 myMembers 表,并包含一个“friend_array”,其中多个 ID 存储在单个字符串中。

另一个表包含我的网站用户所有帖子(推文)的列表。

如何链接两个表以按最新帖子显示用户的所有好友帖子?

// myMember 表

id, friend_array,
1,  1,2
2,  2,5,7
.
.

// posts 表

id, mem_id, post,     post_date
1,  5,      PHP ole,  2011-08-11 11:30
2,  2,      AJAX ole, 2011-08-12 13:10

希望这是有道理的。提前谢谢大家

This is my first question, so hope it makes sense.

How can I link two tables using php/mysql when one contains a string with multiple values.

Basically, one table is myMembers table and contains a "friend_array" where multiple ID's are stored in a single string.

The other table contains a list of all my sites users posts (tweets as such).

How do I link both tables to display all a user's friends posts by the newest post?

// myMember table

id, friend_array,
1,  1,2
2,  2,5,7
.
.

// posts table

id, mem_id, post,     post_date
1,  5,      PHP ole,  2011-08-11 11:30
2,  2,      AJAX ole, 2011-08-12 13:10

Hope this makes sense. Thank you all in advance

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

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

发布评论

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

评论(4

不寐倦长更 2024-12-07 20:04:26

如果我没理解错的

mysql_query("SELECT * FROM POSTS WHERE mem_id IN( SELECT friend_array FROM myMember WHERE id=" . $id);

话 $id 是你希望为其找到朋友的当前成员的 id。

If I'm unstanding correctly

mysql_query("SELECT * FROM POSTS WHERE mem_id IN( SELECT friend_array FROM myMember WHERE id=" . $id);

Where $id is the id of the current member you wish find friends for.

似狗非友 2024-12-07 20:04:26

您可以使用 PHP 轻松完成。你甚至不需要爆炸。取决于你如何做到这一点;它的基础知识是:

    $q = mysqli_query("SELECT * FROM myMember WHERE id = " . $id);
    while($info = mysqli_fetch_array($q)) {
     $p = mysqli_query("SELECT * FROM posts WHERE mem_id IN(".$info['friendArray'].") ORDER BY post_date DESC");
    while($pinfo = mysqli_fetch_array($p) {
echo $pinfo['post_title']; // etc...
}
    }

当然,如果您有数百个帖子,您不想在循环内进行查询,在这种情况下,我发现抓取相关信息并将其存储在两个单独的数组中,然后运行 ​​foreach 循环可以减少密集的。

规范化您的数据库也不会那么密集......其他人在下面/上面发布了建议。

You can do it easily with PHP. You shouldn't even need to explode. Depends how you do this; basics of it are:

    $q = mysqli_query("SELECT * FROM myMember WHERE id = " . $id);
    while($info = mysqli_fetch_array($q)) {
     $p = mysqli_query("SELECT * FROM posts WHERE mem_id IN(".$info['friendArray'].") ORDER BY post_date DESC");
    while($pinfo = mysqli_fetch_array($p) {
echo $pinfo['post_title']; // etc...
}
    }

Granted you don't want to be doing queries inside a loop if you have hundreds of posts, in which case I find grabbing relevant info and storing it in 2 separate arrays, and then running a foreach loop to be less intensive.

Normalising your database would also be less intensive... someone else posted a suggestion to that below/above.

帅哥哥的热头脑 2024-12-07 20:04:25

如果可能的话,我会重组表,以便您得到如下结构:

posts

id, mem_id, post,     post_date
1,  5,      PHP ole,  2011-08-11 11:30
2,  2,      AJAX ole, 2011-08-12 13:10

其中 id 是主键

mymembers

id, name
1,  member1
2,  member2

其中 id 是主键

memberconnections

id, mem_id, friendid
1, 1, 1
2, 1, 2
3, 2, 2
4, 2, 5
5, 2, 7

其中 id 是主键,friendid 是外键

这样您就可以轻松地创建一个查询来获取您想要的信息。

使用查询代替 PHP 的东西是更丰富、更好的,也是实现它的方法。

I would restructure the table if possible so that you get a structure something like the following:

posts

id, mem_id, post,     post_date
1,  5,      PHP ole,  2011-08-11 11:30
2,  2,      AJAX ole, 2011-08-12 13:10

Where id is the primary key

mymembers

id, name
1,  member1
2,  member2

Where id is the primary key

memberconnections

id, mem_id, friendid
1, 1, 1
2, 1, 2
3, 2, 2
4, 2, 5
5, 2, 7

Where id is the primary key and friendid is the foreign key

This way you can easily create a query to get the info you want.

Using a query for this n stead of PHP stuff is fatster and better and the way to do it.

塔塔猫 2024-12-07 20:04:25

如果您想保留数据库结构,可以使用 FIND_IN_SET()< /a> 和一个子查询。这不会像真正的 IN 子句那样分割 CSV 值,但具有相同的效果:

SELECT *
  FROM posts
 WHERE FIND_IN_SET(mem_id,
           (SELECT friend_array FROM myMember WHERE id=2)
       )

If you want to keep your database structure, you can use FIND_IN_SET() and a subquery. That doesn't split up the CSV values like in a real IN clause, but has the same effect:

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