首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何通过匹配#id并收集所有其他数据来连接多个表?

如何通过匹配#id并收集所有其他数据来连接多个表?
EN

Stack Overflow用户
提问于 2011-06-16 23:21:18
回答 3查看 274关注 0票数 0

我有很好的5-7张桌子。然后,我有一个单独的表,其中包含每个表内容的post_id。现在,我想要一个类似于Facebook的用户馈送。那么,我如何加入提要表格,比如说照片、文章、视频。

结构:

// post_id _id=来自所有其他唯一表的内容

代码语言:javascript
运行
复制
User Feed Table:

id
content_id
user_id
link

Posts:

id
post_id
post_content
tags
user_id

links:

id
post_id
link_content
tags
user_id

link_comments:
id
post_comment_id
post_id
tags
user_id
EN

回答 3

Stack Overflow用户

发布于 2011-06-16 23:26:38

您必须使用MYSQL JOIN语句。http://dev.mysql.com/doc/refman/5.1/en/join.html

票数 1
EN

Stack Overflow用户

发布于 2011-06-16 23:32:48

您可以使用join,也可以简单地说content_id = post_id ...在您不知道的情况下指定多个表的方式是这样的……

从提要f、帖子p、链接l、内容c中选择f.*,其中f.content_id = l.post_id等。等等。

票数 1
EN

Stack Overflow用户

发布于 2011-06-16 23:42:01

我建议在提要表格中为“type”添加一个额外的字段。这将是内容的类型。每次有人添加新帖子时,都会在提要表格中插入一条记录,并将content_id设置为该帖子的id和type=1 (1可能表示帖子)。然后,您可以像这样构造一个查询:

代码语言:javascript
运行
复制
// get all records from feed
// then foreach record
// $type_id = feed->type

// then do a switch statement
switch($type_id){
    case 1:
        $type="articles";
        break;
    case 2:
        $type="videos"; 
}

// then do another query joining the feed to the content
SELECT * FROM '$type'
JOIN feed ON '$type'.id = feed.content_id
WHERE '$type'.id = feed->content_id

//execute query and add to an array of feed items
//endforeach

我很抱歉我的语法都错了这更像是一个解决问题的答案...

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6374295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档