Query with HAVING and WHERE

I am trying to create one query that will combine the following two queries.

SELECT 
  campgroundid, 
  ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * 
    cos( radians( lng ) - radians(-122) ) + 
    sin( radians(37) ) * sin( radians( lat ) ) ) ) 
  AS distance 
FROM campground 
HAVING distance < 25 
ORDER BY distance LIMIT 0 , 20;

SELECT * FROM campground WHERE type='private' AND wifi = 1

I tried putting them in IN, but it returned a syntax error, I could not figure out how to fix it. I tried just removing HAVING and concatenating the queries, but then it says that it cannot understand what it is distance. Any help is appreciated. Thank.

OUTPUT: [campgroundid, name, type, wifi, distance] [1, camp ABC, private, 1, 1.34 miles] [2, camp XYZ, private, 1, 4.44 miles]

+3
source share
5 answers

Among the information that is not listed is how the campground tables and markers are related. We will need this information to know how to connect to tables.

, HAVING GROUP BY ( WHERE GROUP BY). , , .

, - :

 SELECT id (expression) as distance FROM markers
      WHERE distance < 25 AND 
        campground_id IN (SELECT id FROM campgrounds WHERE type = 'private' AND wifi = 1)

EDIT: , .

ALIASes WHERE. , , , HAVING, HAVING WHERE. GROUP BY:

SELECT campgroundid, name, private, wifi, 
   ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * 
    cos( radians( lng ) - radians(-122) ) + 
    sin( radians(37) ) * sin( radians( lat ) ) ) ) 
    AS distance 
FROM campground 
GROUP BY campgroundid 
HAVING distance < 25 AND type='private' AND wifi = 1
ORDER BY distance LIMIT 0 , 20;

, campgroundid ( id).

+3

, WHERE distance < 25, HAVING - , HAVING MAX(distance) < 25 .

0

, ( , where sql )

SELECT 
  ... AS distance 
FROM campground 
WHERE type='private' AND wifi = 1
HAVING distance < 25 
ORDER BY distance LIMIT 0 , 20;
0

SELECT campgroundid, (3959 * acos (cos ( (37)) * cos ( ()) *   cos ( (lng) - (-122)) +   sin ( (37)) * sin ( ()))) AS WHERE type = 'private' wifi = 1 ORDER BY distance LIMIT 0, 20 < 25

0

, WHERE JOIN. SELECT. , , SELECT USER.UserName, USER.UserId, LOC.id, LOC.lat, LOC.lon, (3959 * acos (cos ( ('123.1210022')) * cos ( ()) * cos ( () - ('21.200001 ')) + sin ( (' 123.1210022 ')) * sin ( ()))) AS FROM userlocation LOC, < '1' LOC.id = USER.UserId ORDER BY distance LIMIT 0, 20

USER.UserId , LOC.id = USER.UserId WHERE.

-1

Source: https://habr.com/ru/post/1784447/


All Articles