0

I have this query:

$score = 10;
SELECT timecode, count(tag) as n_tags, tag
FROM dados
WHERE dados.tag = 'tag1' 
AND dados.filename = 'file.mp4'
AND (timecode >= '-5' AND timecode <= '15')
AND (timecode = '$score')
GROUP BY timecode
ORDER BY count(tag) DESC

However I want to change the 6 line to:

AND (timecode = '$score' AND n_tags > 3)

but it doesn't seems the correct way to do, it doesn't work.

Any ideas ?

2
  • You are both checking equality timecode = '$score' and checking if it is in a range timecode >= -5 AND timecode <= 15. Is $score inside that range? (so why do you need the range at all?) Commented Feb 20, 2013 at 14:32
  • 1
    Test that $score is between -5 and 15 before making your db query, and only execute the query if it is - then you can eliminate AND (timecode >= '-5' AND timecode <= '15') Commented Feb 20, 2013 at 14:33

2 Answers 2

2

Remove that line from your WHERE clause and put in a HAVING clause. A HAVING clause will look at aggregate results. WHERE does not.

SELECT timecode, count(tag) as n_tags, tag
FROM dados
WHERE dados.tag = 'tag1' 
AND dados.filename = 'file.mp4'
AND (timecode >= '-5' AND timecode <= '15')
AND (timecode = '$score')
GROUP BY timecode
HAVING count(tag) > 3
ORDER BY count(tag) DESC

This line is redundant and could be dropped: AND (timecode >= '-5' AND timecode <= '15')

SELECT timecode, count(tag) as n_tags, tag
FROM dados
WHERE dados.tag = 'tag1' 
AND dados.filename = 'file.mp4'
AND (timecode = '$score')
GROUP BY timecode
HAVING count(tag) > 3
ORDER BY count(tag) DESC
Sign up to request clarification or add additional context in comments.

6 Comments

A HAVING clause will look at aggregate results. WHERE does not.
@Tom The point is that your answer needs to include that information. A solution without an explanation isn't really an answer - it's just, well, a solution.
n_tags is an alias of an aggregated column. do you think alias define on the same level can be used on having clause?
Yes, the alias can be used in the HAVING clause. I generally don't do it that way but it can.
@Tom Correct. I just tested it. (I don't know why this works on mysql) In other RDBMS you can't.
|
2

You can't use aggregated fields in a WHERE clause. count() results are only available after ALL applicable rows have been considered. But the where filtering is done as each individual row is parsed. Cliche statement, but you're trying to count your chickens before they've hatched.

Such things have to be done with a HAVING clause, which is basically exactly the same as a where, but is applied just before the final results are sent back to the client. So...

SELECT ..., count(tag) as n_tags
...
WHERE (timecode = '$score')

HAVING (n_tags > 3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.