- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了以下表格 -
评论:
| column | type |
+-----------+-------------+
| comment_id| int(11) |
| comment | longText |
| parent_id | int(11) |
+-----------+-------------+
评论元:
| column | type |
+-----------+-------------+
| comment_id| int(11) |
| key | varchar(40) |
| value | varchar(50) |
+-----------+-------------+
与 key
comment meta
中的值 table (delete, reply_count, report)
.
数据通过以下方式添加到表中:
comments
中数据库 parent_id
设置为 0。comments
中数据库 parent_id
设置为 comment_id
的评论和 reply_count' is updated in the
该评论的 comments_meta` 数据库。comments_meta
中的值会更新数据库但设置 key
至 delete
和 value
至 1
w.r.t 该评论或回复 ID,而不是将其从 comments
中删除数据库。我只想获取所有未删除评论的列表。
到现在为止我试过这个:
select comments.comment_id,comments.comment, ifnull(comments_meta.value,0) as reply_count from comments left join comments_meta on comments_meta.comment_id = comments.comment_id and comments_meta.meta_key = "reply_count";
这给了我所有评论,包括已删除的评论。
是否可以使用此表模型获取列表?如何获取?或者我应该附加其中一个属性 delete
或 'reply_count with
评论`表?
示例数据注释:
|comment_id | comment | parent_id |
+-----------+-------------+-------------+
| 1 | comment1 | 0 |
| 2 | comment2 | 0 |
| 3 | reply1 | 1 |
| 4 | reply2 | 1 |
| 5 | comment3 | 0 |
+-----------+-------------+-------------+
示例数据注释元:
|comment_id | key | value |
+-----------+-------------+-------------+
| 2 | delete | 1 |
| 1 | reply_count | 2 |
+-----------+-------------+-------------+
预期输出:
|comment_id | comment | reply_count |
+-----------+-------------+-------------+
| 1 | comment1 | 2 |
| 5 | comment3 | 0 |
+-----------+-------------+-------------+
最佳答案
数据链接:
create table `comment`(
`comment_id` int not null auto_increment,
`comment` varchar(128),
`parent_id` int not null,
primary key(`comment_id`)
);
insert into `comment`(`comment_id`,`comment`,`parent_id`) values
(1,'comment1',0),
(2,'comment2',0),
(3,'reply1',1),
(4,'reply2',1),
(5,'comment3',0);
create table `comment_meta`(
`comment_id` int not null,
`key` varchar(128),
`value` int not null,
primary key(`comment_id`,`key`)
);
insert into `comment_meta`(`comment_id`,`key`,`value`) values
(2,'delete',1),
(1,'reply_count',2);
让我们只获取“根”注释:
SELECT * FROM `comment`
WHERE `parent_id` = 0
让我们只选择评论:
SELECT * FROM `comment`
WHERE `parent_id` != 0
让我们加入(外部 - 列出所有根评论)这两个子查询,看看我们得到什么:
SELECT *
FROM
(
SELECT * FROM `comment`
WHERE `parent_id` = 0
) AS `root_comments`
LEFT JOIN
(
SELECT * FROM `comment`
WHERE `parent_id` != 0
) `replies` ON `replies`.`parent_id` = `root_comments`.`comment_id`;
我们得到类似 this 的信息:
现在让我们向结果集中添加一个额外的列,以显示该行是代表根评论 (0) 还是代表回复 (1):
SELECT *,IF(`replies`.`comment_id` IS NOT NULL, 1, 0)
FROM
(
SELECT * FROM `comment`
WHERE `parent_id` = 0
) AS `root_comments`
LEFT JOIN
(
SELECT * FROM `comment`
WHERE `parent_id` != 0
) `replies` ON `replies`.`parent_id` = `root_comments`.`comment_id`;
让我们选择已删除的评论:
SELECT * FROM `comment_meta`
WHERE `key` = 'delete'
将它们与我们目前拥有的结合起来,并添加一个 WHERE
条件以仅考虑那些在元表中没有带有“删除”键的对应行的评论:
WHERE `deleted_comments`.`comment_id` IS NULL
最后,我们只需要选择需要的列和GROUP BY
评论/SUM()
每个评论的回复(1):
请注意,在 MySQL 5.7 中,GROUP BY
默认模式已更改,如果不更改此设置,您将无法SUM
非分组列。
SELECT `root_comments`.`comment_id`,`root_comments`.`comment`,SUM(IF(`replies`.`comment_id` IS NOT NULL, 1, 0)) AS reply_count
FROM
(
SELECT * FROM `comment`
WHERE `parent_id` = 0
) AS `root_comments`
LEFT JOIN
(
SELECT * FROM `comment`
WHERE `parent_id` != 0
) `replies` ON `replies`.`parent_id` = `root_comments`.`comment_id`
LEFT JOIN
(
SELECT * FROM `comment_meta`
WHERE `key` = 'delete'
) `deleted_comments` ON `deleted_comments`.`comment_id` = `root_comments`.`comment_id`
WHERE `deleted_comments`.`comment_id` IS NULL
GROUP BY `root_comments`.`comment_id`;
这是 link通过这个工作示例到 DBFiddle
关于MySql Join条件与Where条件获取数据列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52963344/
我是一名优秀的程序员,十分优秀!