1

I'm trying to get a listing of user id's (uid) that have posted more than 3 articles. Here's my sql:

SELECT n.uid
FROM node
GROUP BY n.uid
HAVING COUNT( n.type = 'article' ) > 3

After doing some reading, I'm guessing my mistake is with my usage of COUNT, but I can't find out what I'm doing wrong. Any hints? Thanks heaps!

1
  • Don't forget to select an answer :).
    – Shibumi
    Mar 29, 2011 at 1:33

5 Answers 5

6

More like...

SELECT uid
FROM node
WHERE type = 'article'
GROUP BY uid
HAVING COUNT(uid) > 3

EDIT: I'll add a bit more info on WHERE vs. HAVING. As far as I remember, HAVING is used to apply conditions post-aggregation by GROUP BY. WHERE is used to apply conditions to the table in general.

0
3
HAVING COUNT( n.type = 'article' ) > 3

Why this doesn't work (re: I can't find out what I'm doing wrong):

  • n.type = 'article' << this returns a BOOLEAN, which is either true/false or 1/0
  • COUNT ( x ) increases the counter whenever x is NOT NULL
  • both of 1/0 are not null, therefore, it counts ALL types, and is equivalent to COUNT(*)

You wanted (this answer has already been given, but repeating here)

SELECT n.uid
FROM node
WHERE n.type = 'article'
GROUP BY n.uid
HAVING COUNT( * ) > 3

In general you could also write your COUNT properly using CASE, but the WHERE filter is normally faster

SELECT n.uid
FROM node
GROUP BY n.uid
HAVING COUNT( CASE WHEN n.type = 'article' THEN 1 END ) > 3
1

COUNT() can only count up grouped records or columns in grouped results; it can't be used to count records that meet a condition in the way you want. Try either subquerying the expression, or pulling it up into the select list where it can be used in conditions:

SELECT n.uid, COUNT(*)
FROM node n
WHERE n.type = 'article'
GROUP BY n.uid
HAVING COUNT(*) > 3

SELECT n.uid
FROM node n
WHERE (SELECT COUNT(*) from node n2 where n2.uid = n.uid AND n.type == 'article')  > 3
3
  • 1
    -1 for COUNT() can only evaluate column names, not conditions. It can evaluate conditions, such as COUNT(Case when .. then 1 else NULL end) Mar 28, 2011 at 21:46
  • ... which wasn't his usage to begin with; he wanted to use Count() in a similar way as .NET Linq, where it returns the number of elements for which a predicate is true. I'll clarify, but please remove your downvote.
    – KeithS
    Mar 28, 2011 at 21:54
  • When I actually downvote, I don't preface with -1 (not normally). -1 in sentiment only, but someone else carried it out. Your edit makes for a really bad query anyway. (the 2nd one, so not sure why it is offerred when the 1st is working, per other answers) Mar 28, 2011 at 22:01
0
SELECT n.id, COUNT(n.id) as my_count
FROM node
WHERE n.type = 'article'
GROUP BY n.id
HAVING my_count > 3
0
SELECT n.uid
FROM node n
--- keep only articles postings
WHERE n.type = 'article'
--- group by user
GROUP BY n.uid
--- count rows while grouping (you can use COUNT(*) instead, result is same)
HAVING COUNT( n.uid ) > 3

Because TRUE=1 and FALSE=0 in MySQL (as Richard explained), your query would work (!) by changing only COUNT to SUM :

SELECT n.uid
FROM node n
GROUP BY n.uid
HAVING SUM( n.type = 'article' ) > 3

But that is not the proper way to do this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.