php - 在 PHP MYSQLI 中将 3 个或更多表链接在一起以创建墙源

标签 php mysql sql join relationship

我一直在尝试将这 5 个表链接在一起,因为我正在创建一个墙提要,其中的帖子是从用户及其 friend 那里获取的。然而,我发现很难从这 5 个表中选择所有数据而不让 mysqli 出现错误。这就是数据库的样子。

我有一个帖子表 This is the posts table, in it it specifies the post id, user id and the type of posts: image, video, or story 我还有一个用户表 The users information is stored here friend table 一起

现在取决于帖子类型:我有一个照片表,因此 The photos table has the post id, user id and other information

问题是我如何将所有这些表链接在一起并将它们输出为墙提要的行。拜托我已经绞尽脑汁了

最佳答案

SELECT p.id, p.post_title, p.post_category, p.post_content, u.username
FROM post_table as p, user_table as u, friends_table as f
WHERE u.user_id = p.user_id and (f.user1 = p.user_id or f.user2 = p.user_id) 

这只是从相关表中获取数据的简单方法。

关于php - 在 PHP MYSQLI 中将 3 个或更多表链接在一起以创建墙源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51163001/

相关文章:

javascript - .load() 与数据

php - 如何在php中的sql查询中使用 session 值?

MySQL 不同计数

php - codeigniter 加入查询问题

php - HTML 元素数组,名称 ="something[]"或名称 ="something"?

php - 我应该将对象或普通数据传递到 View 中吗?

mysql - 列名前的 '0' 的用途

sql - ROW_NUMBER 条查询数据是否需要获取整个结果集?

mysql - 创建 SQL 表,如何将 BIT 条目转换为文本 YES 或 NO?

MySQL删除与表列匹配的所有单词