Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8212375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:44:02+00:00 2026-06-07T10:44:02+00:00

Due to the syntax of mySQL this statement is not allowed (a counted element

  • 0

Due to the syntax of mySQL this statement is not allowed (a counted element cannot be used within the WHERE part):

Illegal syntax:
SELECT COUNT(x) AS amount ... WHERE amount > 0

But how do I handle a COUNTed element via WHERE ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:44:04+00:00Added an answer on June 7, 2026 at 10:44 am

    edit

    Note that your original query may not do what you think it does. You probably want WHERE x > 0 instead:

    SELECT COUNT(x) AS amount ... WHERE x > 0
    

    This is because HAVING amount > 0 is essentially meaningless if there are ANY x values greater than 0. MySQL will automatically group and include all of your rows, counting every one of them. It looks like you want to know the number of rows with a non-zero amount in x, so you need to specify with WHERE that only those rows should match the original criteria to be counted. See below for an example case of needing an aggregate function with HAVING.

    original answer:

    You do this with HAVING. The syntax is exactly the same as WHERE, but it is evaluated after the aggregation.

    SELECT COUNT(x) AS amount ... HAVING amount > 0
    

    The difference is important, since it allows you to limit which rows are counted with WHERE, and then limit the rows returned with HAVING.

    For instance, let’s say I wanted to know how much money I made per department on sales of LESS THAN $10, only if it made more than $1000:

    SELECT 
      SUM(amount) as total_sales
      ... 
    WHERE amount < 10.0 
    GROUP BY department 
    HAVING total_sales > 1000.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize this error is usually due to some syntax or type issues but
I have this MySQL query, and it works on a MySQL database, but not
The following equations are written in Miranda Syntax, but due to the similarities between
Due to a sad situation I receive HTML like this: <p>Perform the following commands:
Due to the lack of documentation for this on MSDN, I am forced to
I'm trying to create my first mysql stored procedure, but script doesn't work due
I am using SSRS2008R2 to report data stored in MySQL database. Due to MYSQL
Ok, so maybe my search syntax is wrong here, this could have very well
So I am wondering if the following is legal, perhaps its not working due
Due to DSolve syntax, systems of differential equations have to be given as lists

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.