需要有关评论审核的 SQL 查询帮助
我正在开发一个评论审核系统作为我网站的一部分,这样我就可以在评论发布之前看到评论列表,这样我就可以正常发布它们,或者在不合适的情况下删除它们。
我的 SQL 工作正常,我可以看到评论的作者是谁、发布日期、评论等,甚至是它所在的页面(无论是新闻页面、博客还是活动页面)。
然而,我无法让它显示页面的标题(新闻标题、博客标题或活动标题)。
这确实很有帮助,所以我希望将其合并到我的查询中。
我的查询现在的工作原理是,它选择评论表中的所有内容,包括物理页面(新闻、博客、事件)。
我想象它要做的是,一旦它知道评论属于哪个页面,就查询该表并获取标题。
有谁知道我该怎么做?选择页面后是否需要嵌套选择?
这是我当前的查询($ get_id)从url获取评论id)
SELECT c . * , ifnull( cc.commentcount, 0 ) AS ccount
FROM comments c
LEFT OUTER
JOIN (
SELECT page, pageid, count( * ) AS commentcount
FROM comments
GROUP BY page
) AS cc ON cc.pageid = c.pageid
WHERE c.commentid = '$get_id'
,这是数据库结构(如果有帮助的话)
CREATE TABLE `comments` (
`commentid` int(5) NOT NULL auto_increment,
`page` varchar(20) NOT NULL default '',
`pageid` int(3) NOT NULL default '0',
`user` varchar(40) NOT NULL default '',
`email` varchar(100) NOT NULL default '',
`website` varchar(100) NOT NULL default '',
`comment` text NOT NULL,
`posted` datetime NOT NULL default '0000-00-00 00:00:00',
`status` enum('0','1') NOT NULL default '0',
PRIMARY KEY (`commentid`)
)
// the tables for news, blogs and events are pretty much the same
CREATE TABLE `news` (
`id` int(4) NOT NULL auto_increment,
`title` varchar(100) NOT NULL default '',
`content` text NOT NULL,
`postdate` date NOT NULL default '0000-00-00',
`photo` varchar(50) NOT NULL default '',
`alternate` varchar(50) NOT NULL default '',
`archived` char(1) NOT NULL default 'n',
`page` varchar(4) NOT NULL default 'news',
PRIMARY KEY (`id`)
)
我不知道这是否有帮助,但这就是我在发布后显示评论的方式
SELECT c.*,
b.title,
ifnull(cc.commentcount,0) as ccount
FROM comments c
INNER
JOIN blogs b
ON c.pageid = b.id
LEFT OUTER
JOIN (SELECT page,pageid,
count(*) as commentcount
FROM comments
GROUP BY page) as cc
on cc.pageid = c.pageid
WHERE c.pageid='$blogid'
编辑:当前查询与建议艾因的变化
SELECT c. * , ifnull( cc.commentcount, 0 ) AS ccount,
CASE c.page
WHEN 'news'
THEN SELECT title
FROM news
WHERE id = c.pageid
WHEN 'blog'
THEN SELECT title
FROM blog
WHERE id = c.pageid
ELSE ''
END FROM comments c
LEFT OUTER JOIN (
SELECT page, pageid, count( * ) AS commentcount
FROM comments
GROUP BY page
) AS cc ON cc.pageid = c.pageid
WHERE c.commentid = '2'
I am developing a comments moderating system as part of my website so i can see a list of comments before they are published so i can either publish them as okay or delete them if inappropiate.
I have haof of my SQL working ok where i can see who the author of the comment is, the date posted, the comment etc and even what page it was on (whether it was a news page, blogs or events page).
What i can't get it to do however is display the title of the page (news title, blog title or event title).
This would really help so i would like it incorporated into my query.
How my query works now is that it select everything in the comments table including the physical page (news, blogs, events).
What i imagine it to do is once it knows what page the comment belongs to, is query that table and get the title.
Has anyone any idea how i do this? Do i need a nested select once the page has been chosen?
Here is my current query (the $get_id) gets the comment id from the url)
SELECT c . * , ifnull( cc.commentcount, 0 ) AS ccount
FROM comments c
LEFT OUTER
JOIN (
SELECT page, pageid, count( * ) AS commentcount
FROM comments
GROUP BY page
) AS cc ON cc.pageid = c.pageid
WHERE c.commentid = '$get_id'
and here is database structure if it helps
CREATE TABLE `comments` (
`commentid` int(5) NOT NULL auto_increment,
`page` varchar(20) NOT NULL default '',
`pageid` int(3) NOT NULL default '0',
`user` varchar(40) NOT NULL default '',
`email` varchar(100) NOT NULL default '',
`website` varchar(100) NOT NULL default '',
`comment` text NOT NULL,
`posted` datetime NOT NULL default '0000-00-00 00:00:00',
`status` enum('0','1') NOT NULL default '0',
PRIMARY KEY (`commentid`)
)
// the tables for news, blogs and events are pretty much the same
CREATE TABLE `news` (
`id` int(4) NOT NULL auto_increment,
`title` varchar(100) NOT NULL default '',
`content` text NOT NULL,
`postdate` date NOT NULL default '0000-00-00',
`photo` varchar(50) NOT NULL default '',
`alternate` varchar(50) NOT NULL default '',
`archived` char(1) NOT NULL default 'n',
`page` varchar(4) NOT NULL default 'news',
PRIMARY KEY (`id`)
)
I don't know if this helps but this is how i display the comments once published
SELECT c.*,
b.title,
ifnull(cc.commentcount,0) as ccount
FROM comments c
INNER
JOIN blogs b
ON c.pageid = b.id
LEFT OUTER
JOIN (SELECT page,pageid,
count(*) as commentcount
FROM comments
GROUP BY page) as cc
on cc.pageid = c.pageid
WHERE c.pageid='$blogid'
EDIT: Current query with proposed changes from Ain
SELECT c. * , ifnull( cc.commentcount, 0 ) AS ccount,
CASE c.page
WHEN 'news'
THEN SELECT title
FROM news
WHERE id = c.pageid
WHEN 'blog'
THEN SELECT title
FROM blog
WHERE id = c.pageid
ELSE ''
END FROM comments c
LEFT OUTER JOIN (
SELECT page, pageid, count( * ) AS commentcount
FROM comments
GROUP BY page
) AS cc ON cc.pageid = c.pageid
WHERE c.commentid = '2'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设绑定(外键或枚举值)是 news.'page'、blogs.'page' 等字段。因此,对每种类型的条目执行 LEFT JOIN 应该做到这一点,并从所有具有专用名称(“AS”约定)的“页面”字段中获取数据,并以编程方式检查该“页面”条目是否为 NULL。其中不为空的包含您正在查找的条目。
I assume the binding (foreign key, or enumerated values) are the news.'page', blogs.'page' etc. fields. So doing a LEFT JOIN to every single type of entry should do it and get from all the 'page' fields with dedicated name ('AS' convention) and doing programaticaly check if any of this 'page' entries are NULL. Which aren't null contains the entry you are looking for.
假设表
comments
中的字段page
包含“新闻”、“博客”等值来标记评论所属的“页面类型”,并且comments.pageid< /code> 是 FK 到适当的表,你可以这样做
Assuming that field
page
in tablecomments
contains values like "news", "blog" etc to mark the "page type" comment belongs to andcomments.pageid
is FK to appropriate table, you could do something like