Solved

MySql Syntax question

Posted on 2014-10-02
3
222 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
  • 2
3 Comments
 
LVL 24

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 24

Expert Comment

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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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