php使用explode和一个包含多个值的字符串链接两个表
这是我的第一个问题,所以希望它有意义。
当一个表包含具有多个值的字符串时,如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我没理解错的
话 $id 是你希望为其找到朋友的当前成员的 id。
If I'm unstanding correctly
Where $id is the id of the current member you wish find friends for.
您可以使用 PHP 轻松完成。你甚至不需要爆炸。取决于你如何做到这一点;它的基础知识是:
当然,如果您有数百个帖子,您不想在循环内进行查询,在这种情况下,我发现抓取相关信息并将其存储在两个单独的数组中,然后运行 foreach 循环可以减少密集的。
规范化您的数据库也不会那么密集......其他人在下面/上面发布了建议。
You can do it easily with PHP. You shouldn't even need to explode. Depends how you do this; basics of it are:
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.
如果可能的话,我会重组表,以便您得到如下结构:
posts
其中 id 是主键
mymembers
其中 id 是主键
memberconnections
其中 id 是主键,friendid 是外键
这样您就可以轻松地创建一个查询来获取您想要的信息。
使用查询代替 PHP 的东西是更丰富、更好的,也是实现它的方法。
I would restructure the table if possible so that you get a structure something like the following:
posts
Where id is the primary key
mymembers
Where id is the primary key
memberconnections
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.
如果您想保留数据库结构,可以使用
FIND_IN_SET()
< /a> 和一个子查询。这不会像真正的IN
子句那样分割 CSV 值,但具有相同的效果: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 realIN
clause, but has the same effect: