SELECT From MySQL View With HAVING Clause Returns Empty Result Set

前端 未结 2 1501
孤城傲影
孤城傲影 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.

    0 讨论(0)
  • 2021-01-19 16:32

    Try this:

    select * from (
        SELECT 
        restaurantName, 
        restaurantID, 
        locationID, 
        locationCity, 
        locationState, 
        locationAddress, 
        locationLatitude, 
        locationLongitude,
        ( 3959 * acos( cos( radians('%s') ) * cos( radians( locationLatitude ) ) * cos( radians( locationLongitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( locationLatitude ) ) ) ) AS distance 
        FROM newView
    ) S
    where distance < '%s' 
    ORDER BY distance
    
    0 讨论(0)
提交回复
热议问题