简体   繁体   English

具有多个Where子句的SQL

[英]SQL with Multiple Where Clauses

First -- I have read about 7 pages of posts with similar titles but couldn't find the right insight for my challenge 首先 - 我已经阅读了大约7页具有相似标题的帖子,但无法找到适合我挑战的正确见解

My SQL: 我的SQL:

SELECT name, address, lat, lng, city, state, phone, zip, info
    , ( 3959 * acos( cos( radians('37.4969') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('-122.2674') ) + sin( radians('37.4969') ) * sin( radians( lat ) ) ) ) AS distance 
FROM myhealthfinder_map 
HAVING distance < '50'  and location = '2'  
ORDER BY distance LIMIT 0 , 10

I get the error message: Invalid query: Unknown column 'location' in 'having clause' 我收到错误消息:无效查询:'having子句'中的未知列'location'

if instead of HAVING I just make it WHERE location = '2' then it works fine [it finds the column] (but I need the distance selector). 如果不是HAVING我只是使它WHERE location ='2'然后它工作正常[它找到列](但我需要距离选择器)。

Any suggestion on how to knock this down? 关于如何打倒这个的任何建议?

Use both WHERE and HAVING. 同时使用WHERE和HAVING。 HAVING is used for aggregated and calculated columns. HAVING用于聚合和计算列。 And WHERE on plain old columns. WHERE上普通的旧列。

SELECT name, address, lat, lng, city, state, phone, zip, info
    , ( 3959 * acos( cos( radians('37.4969') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('-122.2674') ) + sin( radians('37.4969') ) * sin( radians( lat ) ) ) ) AS distance 
FROM myhealthfinder_map 
WHERE location = '2' 
HAVING distance < '50'  
ORDER BY distance LIMIT 0 , 10

More explanation found here WHERE vs HAVING 在这里找到更多解释WHERE vs HAVING

Don't use HAVING without GROUP BY . 不使用GROUP BY不要使用HAVING You can try this instead 你可以试试这个

SELECT name, address, lat, lng, city, state, phone, zip, info, ( 3959 * acos( cos( radians('37.4969') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('-122.2674') ) + sin( radians('37.4969') ) * sin( radians( lat ) ) ) ) AS distance 
  FROM myhealthfinder_map 
 WHERE location = '2' AND
( 3959 * acos( cos( radians('37.4969') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('-122.2674') ) + sin( radians('37.4969') ) * sin( radians( lat ) ) ) ) < 50
 ORDER BY distance LIMIT 0 , 10

It's not pretty, but it should work. 它不漂亮,但它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM