My business partner and I are having issues selecting from a MySQL view that has a HAVING clause.
The query simply selects a few fields from the view, determines a
The HAVING
clause is meant to be used on aggregated data when you are grouping rows together using the GROUP BY
clause. Since you are operating on each row individually, you should replace HAVING
with a WHERE
clause. See this example for details.
Using HAVING on non-aggregate columns in your SELECT list is non-standard behaviour which MySQL supports, but behaviour that shouldn't be relied on. Even the MySQL reference discourages it:
Do not use HAVING for items that should be in the WHERE clause. For example, do not write the following:
SELECT col_name FROM tbl_name HAVING col_name > 0;
Write this instead:
SELECT col_name FROM tbl_name WHERE col_name > 0;
As an aside: if you are passing arguments from the user to your query (with the %s
), make sure you look into prepared statements. Otherwise you may have a glaring security flaw on your hands.