Solved

MySql Syntax question

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

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…
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.​
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now