Solved

MySql Syntax question

Posted on 2014-10-02
3
227 Views
Last Modified: 2014-10-03
The following syntax returns records no problem.  The problem is that the comment counts story comments that haven't been approved so the count is off.  In the notes table there is a field called note_status_id the value 2 means it hasn't been approved.  So trying to figure out with the syntax to include the notes count that only have been approved.

select IFNULL(COUNT(notes.fk_story_id), 0) as commentcnt, 
storys.story_id as storyid, storys.story, story_users.first_name, 
story_users.last_name, storys.title from storys 
inner join story_users on storys.fk_user_id = 
story_users.user_id left join notes on 
storys.story_id = notes.fk_story_id 
where fk_status_id = 2 group by storys.story_id order by storyid desc

Open in new window

0
Comment
Question by:stargateatlantis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 500 total points
ID: 40358432
You have two options: either exclude these notes from the selection completely:
select IFNULL(COUNT(notes.fk_story_id), 0) as commentcnt, 
storys.story_id as storyid, storys.story, story_users.first_name, 
story_users.last_name, storys.title 
from storys 
inner join story_users on storys.fk_user_id = 
story_users.user_id left join notes on 
storys.story_id = notes.fk_story_id 
where fk_status_id = 2 and notes.note_status_id <> 2
group by storys.story_id 
order by storyid desc

Open in new window

or do not count them:
select IFNULL(COUNT(CASE WHEN notes.note_status_id <> 2 THEN notes.fk_story_id END), 0) as commentcnt, 
storys.story_id as storyid, storys.story, story_users.first_name, 
story_users.last_name, storys.title 
from storys 
inner join story_users on storys.fk_user_id = 
story_users.user_id left join notes on 
storys.story_id = notes.fk_story_id 
where fk_status_id = 2 
group by storys.story_id 
order by storyid desc

Open in new window

0
 

Author Comment

by:stargateatlantis
ID: 40358536
So basically do not count them when the status is 2 correct
0
 
LVL 25

Expert Comment

by:chaau
ID: 40358555
Yes. have you tried the queries? Do they produce the desired result?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question