SELECT From MySQL View With HAVING Clause Returns Empty Result Set

前端 未结 2 1502
孤城傲影
孤城傲影 2021-01-19 15:38

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

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-19 16:25

    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.

提交回复
热议问题