"嗜好" PHP / MySQL中的个人评论

时间:2015-02-28 01:06:58

标签: php mysql

我为幻想电影联盟建立了一个网站,我使用PHP和MySQL运行(类似于幻想足球,但用于电影)。每个电影的个人资料页面都有一个可以留下评论的地方,我可以正常使用该部分。现在,我想添加"喜欢"个人评论,但我遇到了一些麻烦。我想我有#34;喜欢"表格设置正确,我可以为每个评论添加相似内容,但是在每个评论中显示内嵌的内容是另一个故事。

以下是"评论"的代码。表:

CREATE TABLE `comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`movie_id` int(11) NOT NULL,
`player_id` tinyint(2) NOT NULL,
`date_time` datetime NOT NULL,
`comment` text CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

以下是"喜欢"的代码表:

CREATE TABLE `likes` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`player_id` int(2) NOT NULL,
`comment_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `player_comment` (`player_id`,`comment_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

以下是"玩家的代码"表:

CREATE TABLE `players` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(10) NOT NULL,
`username` char(10) NOT NULL,
`password` char(32) NOT NULL,
`email` varchar(100) NOT NULL DEFAULT '',
`active` tinyint(1) NOT NULL,
`admin` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

现在,这是我用来获取任何特定电影的所有评论的SQL语句:

SELECT comments.id as comment_id, comments.player_id, comments.date_time, comments.comment,
players.first_name
FROM comments
LEFT JOIN players on players.id = comments.player_id
WHERE movie_id = $movie_id
ORDER BY date_time ASC

我的问题是,我不确定如何在同一声明中获取每条评论的所有相似内容。我希望能够显示每位玩家喜欢该特定评论的名字。所以我希望它看起来像这样:

  • 第1行:$ comment
  • 第2行:$ commenter_first_name
  • 第3行:$ liker_first_name1,$ liker_first_name2,$ liker_first_name3赞这个

我能够像以下那样获得所有喜欢的评论:

SELECT first_name
FROM likes
LEFT JOIN players on players.id = likes.player_id
WHERE comment_id = 264
ORDER BY first_name ASC

但我不知道如何在不使用实际的comment_id的情况下合并它。

希望这是有道理的,我欢迎任何可能的帮助。

2 个答案:

答案 0 :(得分:3)

如下:

SELECT comments.id as comment_id, comments.player_id, comments.date_time, comments.comment, likers.names
FROM comments
LEFT JOIN
    (SELECT comment_id, GROUP_CONCAT(first_name) AS names FROM likes 
    INNER JOIN players ON likes.player_id = players.id GROUP BY comment_id) AS likers 
ON comments.id = likers.comment_id
WHERE movie_id = $movie_id
ORDER BY date_time ASC

"喜欢"表格应该有:

| (comment_id) | Bob,Jim,Kathy |

要获得更好的列表格式,您可以指定分隔符:

GROUP_CONCAT(first_name SEPARATOR ', ') 

答案 1 :(得分:0)

你不能只是将链接添加到评论表吗?

CREATE TABLE `comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`movie_id` int(11) NOT NULL,
`player_id` tinyint(2) NOT NULL,
`date_time` datetime NOT NULL,
`comment` text CHARACTER SET utf8 NOT NULL,
`likes` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

然后当有人喜欢评论时,你只需在评论数据库中添加+1,而不仅仅是喜欢DB。当有人移除他的喜欢时,你会将数字改为-1

像这样:

"UPDATE comments SET likes=likes+1 WHERE id='$comment_id'";

我看到的问题是,你想展示喜欢的......

所以也许是这样的:

"SELECT comments.*, likes.* FROM comments INNER JOIN likes comments.id=likes.comment_id WHERE movie_id='$movie_id'";

您的选择:

"SELECT comments.id as comment_id, comments.player_id, comments.date_time, comments.comment, players.first_name, likes.player_id
FROM comments
LEFT JOIN players ON players.id=comments.player_id
INNER JOIN likes ON comments.id=likes.comment_id 
WHERE movie_id='$movie_id'
ORDER BY date_time ASC";

另一件事是,它只会返回喜欢评论的人的ID。所以我建议你将用户名也保存到喜欢的表中。