Jump to content

[SOLVED] Query help


CroNiX

Recommended Posts

I need some help with a query that I haven't done before and have not yet succeeded.

 

I have 2 tables.

 

blog          blog_comments
-----          ------------
id              id
entry         blog_id
...              visible

I am trying to:

1) select everything from blog

2) count comments left for each blog.entry

3) count comments left for each blog.entry where visible = 0

I got 1 and 2 accomplished using the following statement and need help getting #3.

SELECT
    blog.id,
    blog.entry,
    Count(blog_comments.id) AS comment_total,
FROM
    blog
    Left Join blog_comments ON blog.id = blog_comments.blog_id
GROUP BY
    blog.id,
    blog.entry
ORDER BY
    blog.`date` DESC

Obviously the tables have a lot more fields, but this is the needed info to accomplish what I am after.  If anybody could help I would appreciate it.  Temporarily I am running 2 querries to get the data I need for #3 but would like to just have 1 statement and would like to learn this.

 

Thanks for any insight!

Link to comment
https://forums.phpfreaks.com/topic/143960-solved-query-help/
Share on other sites

hum...is visible either 1 or 0...what about this:

SELECT
    blog.id,
    blog.entry,
    Count(blog_comments.id) AS comment_total,
    COUNT(blog_comments.visible) - SUM(blog_comments.visible) AS nonvisible_total
FROM
    blog
    Left Join blog_comments ON blog.id = blog_comments.blog_id
GROUP BY
    blog.id,
    blog.entry
ORDER BY
    blog.`date` DESC

Link to comment
https://forums.phpfreaks.com/topic/143960-solved-query-help/#findComment-755401
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.