Group by and Having clause in the same query - mysql

Schema:
place(pid, name, type, lat, lng, deleted)
I want to select count of places, grouping them by their type and
having a distance of < 10 KM from a particular lat, lng
Query:
SELECT count(p.type) as count
FROM (place as p)
where p.deleted != 1
and p.pid in
(
select p2.pid,
IFNULL(acos(sin((18.5236 *pi()/180)) * sin((p2.lat*pi()/180))+cos((18.5236 *pi()/180)) * cos((p2.lat *pi()/180)) * cos(((73.8478 - p2.lng)*pi()/180))) * 6371.009, 0) AS distance
from place p2
having `distance` < 10
)
group by p.type;
Error:
Operand should contain 1 column(s)
That is because I am selecting 2 columns i.e pid and distance in the sub select query. But without using a 2nd select column how can I calculate the distance.

Rewrite your script like this
SELECT count(p.type) AS count,
-- remove this if not necessary
SUM(IFNULL(acos(sin((18.5236 *pi()/180)) * sin((p.lat*pi()/180))+cos((18.5236 *pi()/180)) * cos((p.lat *pi()/180)) * cos(((73.8478 - p.lng)*pi()/180))) * 6371.009, 0)) AS distance
FROM place AS p
WHERE p.deleted != 1
GROUP BY p.type
HAVING SUM(IFNULL(acos(sin((18.5236 *pi()/180)) * sin((p.lat*pi()/180))+cos((18.5236 *pi()/180)) * cos((p.lat *pi()/180)) * cos(((73.8478 - p.lng)*pi()/180))) * 6371.009, 0)) < 10

Related

Count locations withing given distance

I have the following query, that selects places withing given radius:
SELECT *
FROM (
SELECT
group,
name,
111.1111 *
DEGREES(ACOS(LEAST(1.0, COS(RADIANS(".$lat."))
* COS(RADIANS(t1.lat))
* COS(RADIANS(".$lon.") - RADIANS(t1.lon))
+ SIN(RADIANS(".$lat."))
* SIN(RADIANS(t1.lat))))) AS distance
FROM t1
) AS grp
WHERE distance < 10
Is there a way to count how many locations in each group withing this query?
In MySQL you can use HAVING to reduce the number of rows.
SELECT
`group`, COUNT(*) NumOfNames
FROM
(SELECT
`group`,
name,
ST_DISTANCE_SPHERE(POINT('.$lon.', '.$lat.'), POINT(t1.lon, t1.lat)) / 1000 AS distance
FROM
t1
HAVING distance < 10) t2
GROUP BY `group`

Is it possible to use GROUP BY with GROUP_CONCAT on same column with limit condition

SELECT id,businessName,GROUP_CONCAT(id) FROM tbl_business LIMIT 0,1
Above query is giving me below results
1 Camp San Fransisco 1,4
Now if i apply GROUP BY and limit on same query, GROUP_CONCAT column is not giving me same results as before.
SELECT id,businessName,GROUP_CONCAT(id) FROM tbl_business GROUP BY id LIMIT 0,1
Query Results
1 Camp San Fransisco 1
I'm using GROUP BY id for fetching a unique records with some where condition.
Is there are any alternatives for using GROUP_BY with GROUP_CONCAT or any other methods for MySQL which can be usefull here.
I want to preserve first query results with id so i just have to call second query based on first results id if it is possible. I can not remove GROUP BY id because of multiple table's LEFT JOIN and some SUB QUERY are depends on it
Above queries are just for explanation purpose , original query is much longer.
Actual Query :
SELECT bs.id,bs.businessName,bs.businessDesc,bs.latitude,bs.longitude,bs.slug,bs.contactNo,bs.location,bs.isAddedBy,bs.createdDate,cat.categoryName as category,GROUP_CONCAT(DISTINCT(bcat.categoryName) SEPARATOR ', ') as subcategory,
(SELECT COUNT(id) FROM tbl_reviews WHERE referenceId = bs.id) as totalReview,
(SELECT CEIL(AVG(rating)) FROM tbl_reviews WHERE referenceId = bs.id) as avgRating,
(SELECT imageName FROM tbl_business_images as bimg WHERE bimg.businessId = bs.id LIMIT 1) as bImage,
(
(ACOS
(
SIN(37.09024 * PI() / 180) * SIN(bs.latitude * PI() / 180) + COS(37.09024 * PI() / 180) * COS(bs.latitude * PI() / 180) * COS(
(-95.712891 - bs.longitude) * PI() / 180)
)* 180 / PI()
) * 60 * 1.1515 * 1.609344
) as distance,
(SELECT
(DATEDIFF(bs.createdDate,NOW()) * 5) +
((SELECT COUNT(id) FROM tbl_business_images WHERE businessId = bs.id) * 2) +
((SELECT count(id) FROM tbl_business_views WHERE businessId = bs.id) * 2) +
((SELECT COUNT(id) FROM tbl_reviews WHERE referenceId = bs.id) * 10))
as totalPoint
FROM tbl_business as bs
LEFT JOIN tbl_category as cat
ON bs.businessCat = cat.id
LEFT JOIN tbl_business_subcategories as bsubs
ON bs.id = bsubs.businessId
LEFT JOIN tbl_category as bcat
ON bsubs.subcategoryId = bcat.id
WHERE bs.isActive = 'y'
GROUP BY bs.slug
Current Query Output :
id businessName businessDesc latitude longitude slug contactNo location isAddedBy createdDate category subcategory totalReview avgRating bImage distance totalPoint
1 test test 45.14 -122.82 test 1234567890 USA c 2018-09-29 11:38:58 Category Subcategory1,subcategory2 1 5 image.png 2382.763331498852 -4
Any help is appreciated.

Combining SQL Google Search with own SQL query

I am pretty new to SQL queries.
I have a google SQL Search example
SELECT cID,
(6371 * acos
(
cos(radians(51.455643))
* cos(radians(latCord))
* cos(radians(longCord) - radians(7.011555))
+ sin(radians(51.455643))
* sin(radians(latCord))
)
) AS distance
FROM breitengrade
HAVING distance < 50
ORDER BY distance
LIMIT 0, 20
and a own SQL query
SELECT breitengrade.cID
,breitengrade.latCord
,breitengrade.longCord
,Pages.cIsActive
FROM breitengrade
INNER JOIN Pages ON breitengrade.cID = Pages.cID
WHERE cIsActive = '1'
How can I combine these 2 queries into one so that I can get one single result set?
SELECT breitengrade.cID,
breitengrade.latCord,
breitengrade.longCord,
Pages.cIsActive
(6371 * acos
(
cos(radians(51.455643))
* cos(radians(latCord))
* cos(radians(longCord) - radians(7.011555))
+ sin(radians(51.455643))
* sin(radians(latCord))
)
) AS distance
FROM breitengrade
INNER JOIN Pages ON breitengrade.cID = Pages.cID
WHERE cIsActive = '1'
HAVING distance < 50
ORDER BY distance
LIMIT 0, 20

How to search for events in 5 km radius

I'm trying to select events in a 5 km radius and order them by likes and from oldest to newest. I know how to select events and order them by likes, but I can't figure this one out. The problem maybe is that the location is stored in a different table.
Database structure:
event plusone (like) location
id_event id_plusone id_location
location_id event_id lat
... user_id lon
... city_id
full_name
....
The SQL statement I'm trying:
SELECT 3956 * 2 * ASIN(
SQRT(POWER(SIN((50.127946 - abs(l.lat)) * pi()/180 / 2),2) + COS(50.127946 * pi()/180 ) *
COS(abs(l.lat) * pi()/180) * POWER(SIN((14.4882912 - l.lon) * pi()/180 / 2), 2) )) AS distance,e.*,COUNT( p.id_plusone ) AS
likes
FROM
event e,location l
LEFT JOIN plusone p ON p.event_id = e.id_event
WHERE
e.location_id = l.id_location AND distance<=5 GROUP BY e.id_event
ORDER BY e.timestamp DESC,likes DESC
The huge column at the beginning is calculation of distance of the event from user position using latitude and longitude.
This is what I get from the SQL debugger:
#1054 - Unknown column 'distance' in 'where clause'
Does anybody know how to do this?
Is hard to track where the error is, you should try to solve the distance part first.
SELECT
event_id,
3956 * 2 * ASIN(SQRT( POWER(SIN((50.127946 - abs(l.lat)) * pi()/180 / 2),2)
+ COS(50.127946 * pi()/180 ) * COS(abs(l.lat) * pi()/180) * POWER(SIN((14.4882912 – l.lon) * pi()/180 / 2), 2) )) AS distance,
FROM event e
JOIN location l
WHERE distance <= 5
And then join to back to the event table
SELECT e.*, count(p.id_plusone)
FROM event e
LEFT JOIN plusone p
ON p.event_id = e.id_event
JOIN ( Previous Distance Query) d
ON e.event_id = d.event_id
GROUP BY e.id_event
ORDER BY e.timestamp DESC, likes DESC
and btw, this group_by will bring you event_id plus random values for e.* so be carefull
$latitudeD= latitude;
$longitudeD = longitude;
SELECT event,
SQRT(
POW(69.1 * (coordX - $latitudeD), 2) +
POW(69.1 * ($longitudeD - coordY) * COS(coordX / 57.3), 2)) AS distance
FROM db.table WHERE SQRT(
POW(69.1 * (coordX - $latitudeD), 2) +
POW(69.1 * ($longitudeD - coordY) * COS(coordX / 57.3), 2)) < 50 ORDER BY distance
;

COUNT(*) and Having

The following query gets me a column of distances.
But what i need is only the count of results with matching distances, not the distances themselves. A Subselect cannot be used.
SELECT
( 6368 * SQRT(2*(1-cos(RADIANS(loc_lat)) * cos(0.899945742869) * (sin(RADIANS(`loc_lon`)) * sin(0.14286767838) + cos(RADIANS(`loc_lon`)) * cos(0.14286767838)) - sin(RADIANS(loc_lat)) * sin(0.899945742869))) ) AS Distance
FROM ...
WHERE ...
HAVING Distance > 0 AND Distance <= 25
You just need to move the distance calculation to the where clause:
SELECT COUNT(*) FROM ...
WHERE ( 6368 * SQRT(2*(1-...) BETWEEN 0 AND 25
If you don't need the distances, only the count, maybe this will work:
SELECT Count(*)
FROM ...
WHERE ... AND
(6368 * SQRT(2*(1-cos(RADIANS(loc_lat)) * cos(0.899945742869) *
(sin(RADIANS(`loc_lon`)) * sin(0.14286767838) + cos(RADIANS(`loc_lon`)) *
cos(0.14286767838)) - sin(RADIANS(loc_lat)) * sin(0.899945742869)))
) BETWEEN 0 AND 25
This will give the totalResults, and you can discard the other column.
SELECT COUNT(*) totalResults,
( 6368 * SQRT(2*(1-cos(RADIANS(loc_lat)) * cos(0.899945742869) * (sin(RADIANS(`loc_lon`)) * sin(0.14286767838) + cos(RADIANS(`loc_lon`)) * cos(0.14286767838)) - sin(RADIANS(loc_lat)) * sin(0.899945742869))) ) AS Distance
FROM ...
WHERE ...
HAVING Distance > 0 AND Distance <= 25