Solved

MySql Syntax question

Posted on 2014-10-02
3
224 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 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 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