简体   繁体   English

获取带有喜欢和评论的帖子-一个查询?

[英]Get Posts with likes and comments - one query?

I am about to develop a wall for a users-profile. 我将为用户个人资料开发隔离墙。 It basically works the same way as Facebooks comment/like system. 它的工作原理基本上与Facebook的评论/赞系统相同。

So what I want to do is fetch all wall-posts associated with a users profile (by current id variable) and all comments and likes associated with this specific post. 所以我想做的是获取与用户个人资料关联的所有墙贴(通过当前id变量)以及与此特定帖子关联的所有评论和喜欢。 I want to use user data from both comments and likes (ie.: name, user_id, email, whatever). 我想同时使用评论和喜欢的用户数据(例如:姓名,user_id,电子邮件等)。

So the question is: Is it possible? 所以问题是:可能吗?

According to this thread's answer it is not: Fetch all news and all comments 根据此主题的答案,不是: 获取所有新闻和所有评论

According to this thread it is: mysql/php: show posts and for each post all comments 根据这个线程是: mysql / php:显示帖子,对于每个帖子,所有注释

I have tried working with this code but it doesn't seem to work, as it just fetches all comments from DB. 我曾尝试使用此代码,但似乎不起作用,因为它只是从数据库中获取所有注释。

SELECT p.*, c.*, l.*, u.name as post_author, u2.name as comment_author, u3.user_id
FROM edu_wall p
LEFT JOIN edu_comments c ON c.comment_entity = p.post_id
LEFT JOIN edu_likes l ON l.like_entity = p.post_id
LEFT JOIN edu_users u ON u.user_id = p.post_author
LEFT JOIN edu_users u2 ON u2.user_id = c.comment_author
LEFT JOIN edu_users u3 ON u3.user_id = l.like_author

So what is the solution? 那么解决方案是什么? Should I start up by fetching all wall posts, and then inside that while(), loop comments and likes? 我应该首先获取所有墙贴,然后在while()内循环注释和点赞吗? It just seems like a very complex and resource-intensive solution. 这似乎是一个非常复杂且占用大量资源的解决方案。

Thanks in advance! 提前致谢!

You can use union for example : 您可以使用工会例如:

SELECT id, p.name, NULL as "type", u1.username 
FROM edu_wall p 
LEFT JOIN users u1 ON u.id = p.user_id
UNION
SELECT id, NULL as 'name', e.type, u2.username 
FROM edu_likes e 
LEFT JOIN users u2 ON u.id = e.user_id

Null is to replace extra columns between tables, so here when type is NULL then the infos are for edu_wall user and when it's note they are for edu_likes user. Null是替换表之间的多余列,因此在这里,当type为NULL时,则信息针对edu_wall用户,并且在注意它们针对edu_likes用户时。

You should try and see youself 你应该尝试看看自己

This is certainly possible in a single query. 在单个查询中肯定可以做到这一点。

Without knowing your table layouts and what fields you want to use I can't really give any suggested code at the moment. 在不知道您的表布局以及您想使用哪些字段的情况下,我目前无法提供任何建议的代码。

However the issue is that a single wall post will have numerous comments and numerous likes. 然而,问题在于,单个墙贴将有大量评论和喜欢。 You current SQL would bring back one row for every combination of comment and like for each wall post. 您当前的SQL将为注释的每种组合和每条墙贴一样返回一行。 So if a wall post had 2 comments and 2 likes that would result in 4 rows. 因此,如果墙上的帖子有2条评论和2个赞,则将导致4行。

You can get round this a couple of ways. 您可以通过两种方法解决此问题。 Either make sure you have an order clause on the SELECT and then manually in php process each row and decide if the comment or like is a new one for that wall post. 确保您在SELECT上有一个order子句,然后在php中手动处理每一行,并确定该墙贴是否有新的评论或类似内容。 If so process it and if not ignore it. 如果是这样,请处理它,如果不忽略它。

Alternatively you can use aggregate functions, such as GROUP_CONCAT, to bring back the fields you want in one row. 或者,您可以使用聚合函数(例如GROUP_CONCAT)将您想要的字段带回一行。 Then explode them out in php. 然后在php中将它们炸开。 Sql something like this:- SQL像这样:

SELECT p.*, u.name as post_author, GROUP_CONCAT(u2.name SEPARATOR '#') as comment_authors, GROUP_CONCAT(u3.user_id SEPARATOR '#') as post_likers
FROM edu_wall p
LEFT JOIN edu_comments c ON c.comment_entity = p.post_id
LEFT JOIN edu_likes l ON l.like_entity = p.post_id
LEFT JOIN edu_users u ON u.user_id = p.post_author
LEFT JOIN edu_users u2 ON u2.user_id = c.comment_author
LEFT JOIN edu_users u3 ON u3.user_id = l.like_author
WHERE p.id = $SomeWallId
GROUP BY p.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 只需一次查询即可获取特定Facebook帖子的赞,帖子和次数 - Get number of likes, posts and commets of specific Facebook post with one query 如何以详细的格式获取评论的“喜欢”,例如php中的帖子 - How to get comments' 'likes' in detailed format like posts are having in php 使用 neo4jphp 获取用户帖子,包括喜欢和评论 - use neo4jphp to get user posts, with likes and comments Facebook-获得用户的喜欢和评论 - Facebook - get user likes and comments 如何仅通过一个SQL查询打印帖子和评论 - How to print posts and comments with only one sql query 我试图从 Instagram 用户那里获取一些详细信息(用户名、描述、帖子数、总喜欢和总评论)* - I am trying to get some details ( username, description, Posts Count, total likes & total comments ) from Instagram user* MYSQL-左联接可从数据库表中收集帖子和喜欢/评论 - MYSQL - Left join to gather posts and likes/comments from database tables 有效地检索Facebook帖子(PHP)上的点赞/分享/评论 - Efficiently retrieve likes/shares/comments on facebook posts (PHP) 选择按xxx desc的最高喜欢/不喜欢顺序评论的帖子 - select posts with comments with highest likes/dislikes order by xxx desc 选择带有评论和回复的故事,并在一个查询中检查当前用户是否喜欢 - select the story with its comments and replies and check if current user likes or not in one single query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM