开发者

Need help with SQL query for comments moderation

开发者 https://www.devze.com 2023-03-22 05:03 出处:网络
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 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开发者_StackOverflow社区(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'


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.

SELECT c . * ,n.`title` as `newstitle`, b.`title` as `blogstitle`, e.`title` as `eventstitle`, 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
    LEFT JOIN 'news' n ON n.'pageid' = c.'pageid'
    LEFT JOIN 'blogs' b ON b.'pageid' = c.'pageid'
    LEFT JOIN 'events' e ON e.'pageid' = c.'pageid'
    WHERE c.commentid = '$get_id'


Assuming that field page in table comments contains values like "news", "blog" etc to mark the "page type" comment belongs to and comments.pageid is FK to appropriate table, you could do something like

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
0

精彩评论

暂无评论...
验证码 换一张
取 消