简体   繁体   English

选择当前用户MySQL PHP的批准注释和未批准注释

[英]Select approved comments and unapproved comments of current user MySQL PHP

I'm new to mysql and I have a problem with selecting data from mysql database: 我是mysql新手,从mysql数据库中选择数据时遇到问题:

$post_id = 3;
$current_user_id = 1;

$query = "SELECT `comments`.*, `users`.`username`, `users`.`image` FROM (`comments`)
LEFT JOIN `users` ON `comments`.`user_id` = `users`.`id`
WHERE `comments`.`post_id` =  '$post_id'
AND `comments`.`status` =  1
AND `users`.`status` =  1
ORDER BY `comments`.`date` desc";

This select, selects all approved comments from database, but also, in this select, I need all unapproved comments of $current_user_id, 此选择从数据库中选择所有批准的注释,但是在此选择中,我需要$ current_user_id的所有未批准的注释,

Result must look like: 结果必须如下所示:

[all approved post comments] + [all unapproved post comments of $current_user_id] [所有批准的帖子评论] + [$ current_user_id的所有未批准帖子评论]


not sure if this will really work, but just give a try and see. 不知道这是否真的有效,请尝试一下。 I can't test the query since we don't have the schema. 由于没有架构,因此无法测试查询。

SELECT `comments`.*, `users`.`username`, `users`.`image` FROM (`comments`)
LEFT JOIN `users` ON `comments`.`user_id` = `users`.`id`
WHERE `comments`.`post_id` =  '$post_id'
AND (
   (`comments`.`status` =  1 AND `users`.`status` =  1)
  OR
   ( `comments`.`status` =  0 AND `users`.`id`= '$current_user_id' )
)
ORDER BY `comments`.`date` desc

What I thought is select all the approved comments OR ( comments that are not approved, but from this user ). 我认为是选择所有已approved comments或(未批准,但来自此用户的评论)。 You might have to alter the query till you get what you really needed, I'm just giving you the idea, not the exact query. 您可能需要更改查询,直到获得真正需要的内容为止,我只是为您提供想法,而不是确切的查询。 Hope this will help you. 希望这会帮助你。

SELECT `comments`.*, `users`.`username`, `users`.`image` FROM (`comments`)
LEFT JOIN `users` ON `comments`.`user_id` = `users`.`id`
WHERE `comments`.`post_id` =  '$post_id'
AND `comments`.`status` =  1
AND `users`.`status` =  1
UNION ALL
SELECT `comments`.*, `users`.`username`, `users`.`image` FROM (`comments`)
LEFT JOIN `users` ON `comments`.`user_id` = `users`.`id`
WHERE `comments`.`post_id` =  '$post_id'
AND `comments`.`status` =  0
AND `users`.`id` = '$current_user_id'
ORDER BY `comments`.`date` desc

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM