I have the following SQL statement:
SELECT Distinct(Venue_Name), Count(*) as `num1`
FROM venuetagview
INNER JOIN
(
SELECT Tag,COUNT(*) AS `num`
FROM usertags
WHERE UserID=4
GROUP BY Tag
ORDER BY `num` DESC
) as a
ON venuetagview.Tag=a.Tag
GROUP BY Venue_Name
ORDER BY `num1` DESC
And I am trying to add a 'distance' calculation into it, using the latitude/longitude I have in the venuetagview table. I have tried this:
SELECT
( 6371
* acos( cos( radians(51.529099) )
* cos( radians( Venue_Latitude ) )
* cos( radians( Venue_Longitude )
- radians(-0.084981) )
+ sin( radians(51.529099) )
* sin( radians( Venue_Latitude ) ) ) ) AS distance,
Distinct(Venue_Name),
Count(*) as `num1`
FROM venuetagview
INNER JOIN
(
SELECT Tag,
COUNT(*) AS `num`
FROM usertags
WHERE UserID=4
GROUP BY Tag
ORDER BY `num` DESC
) as a
ON venuetagview.Tag=a.Tag
GROUP BY Venue_Name
ORDER BY `num1` DESC
However I am getting an error using this.
Only issue I see with your posted query is
distinct is keywork and should be used like distinct column_name
SELECT Distinct (Venue_Name) should just be SELECT Venue_Name or
SELECT Distinct Venue_Name
As mentioned by Strawberry removing DISTINCT solved this:
SELECT
( 6371 * acos( cos( radians(51.529099) )
* cos( radians( Venue_Latitude ) )
* cos( radians( Venue_Longitude )
- radians(-0.084981) )
+ sin( radians(51.529099) )
* sin( radians( Venue_Latitude ) ) ) ) AS distance,
Venue_Name,
Count(*) asnum1
FROM venuetagview
INNER JOIN
(
SELECT Tag,
COUNT(*) ASnum
FROM usertags
WHERE UserID=4
GROUP BY Tag
ORDER BYnumDESC) as a
ON venuetagview.Tag=a.Tag
GROUP BY Venue_Name
ORDER BY distance ASC
Related
I'm trying to get * from the users2 table where the user's location is within the given radius.
The location query works fine on the user_location2 table.
SELECT uid, ( 3959 * acos( cos( radians(28.247800068217) ) * cos( radians( `lat` ) ) * cos( radians( `lon` ) - radians(-80.726205977101) )
+ sin( radians(28.247800068217) ) * sin( radians( `lat` ) ) ) )
AS distance FROM user_location2
HAVING distance <= 25 ORDER BY time_stamp
and the inner join works fine without the location subquery
SELECT *
FROM users2
LEFT JOIN user_location2
ON user_location2.uid = users2.id
I'm just having trouble combining the two. Here's my current query that just is returning all rows, so I'm obviously doing something wrong.
SELECT *
FROM users2
LEFT JOIN user_location2
ON user_location2.uid = users2.id
WHERE EXISTS (SELECT NULL, ( 3959 * acos( cos( radians(26.247800068217) ) * cos( radians( `lat` ) ) * cos( radians( `lon` ) - radians(-89.726205977101) ) + sin( radians(26.247800068217) ) * sin( radians( `lat` ) ) ) )
AS distance FROM user_location2
HAVING distance <= 5 ORDER BY time_stamp)
Edit included
I'm hoping to add in a 3rd table (user_like) to eliminate a lot of possible rows that shouldn't be included in the result.
Let's say the script is running for user_id = 88
So basically users 89, 90 and 91 would fall under the location radius, but wouldn't be included in the result because user 88 already liked them.
Try this...
SELECT users2.*
FROM users2
LEFT JOIN user_location2
ON user_location2.uid = users2.id
WHERE ( 3959 * acos( cos( radians(28.247800068217) ) * cos( radians( `lat` ) ) * cos( radians( `lon` ) - radians(-80.726205977101) )
+ sin( radians(28.247800068217) ) * sin( radians( `lat` ) ) ) ) < = 5
ORDER BY time_stamp
The join is fine between the two tables. The calculated column has been added in the select clause and the where clause because the filter requires it there. It would be easier to put that through a view so that if you need to change it, it can be done in one place.
EDIT: Removed the calculation from SELECT because I believe you don't need to see that. Just left it in the WHERE clause since it needs to be filtered on.
You can try this :
SELECT *
FROM users2 A
LEFT JOIN user_location2 B
ON B.uid = A.id
WHERE ( 3959 * acos( cos( radians(26.247800068217) ) * cos( radians( `B.lat` ) ) * cos( radians( `B.lon` ) - radians(-89.726205977101) ) + sin( radians(26.247800068217) ) * sin( radians( `B.lat` ) ) ) ) > 25;
I have a query like this -
SELECT e.id FROM event e WHERE e.startdatetime<NOW() AND e.isEventDeleted=FALSE AND e.isNeighborlyInvited=TRUE AND e.organized_by!=49 AND e.event_address IN (SELECT id FROM address a WHERE latitude!='' AND longitude!='' AND IFNULL(( 3959 * ACOS( COS( RADIANS(22.6979425) ) * COS( RADIANS( latitude ) ) * COS( RADIANS( longitude ) - RADIANS(75.8597305) ) + SIN( RADIANS(22.6979425) ) * SIN( RADIANS( latitude ) ))),0)<100) AND e.id NOT IN (SELECT eventid FROM event_interest WHERE approvalStatus!='InterestExpressed' AND interested_user=49) AND e.id NOT IN (SELECT eventid FROM event_invite WHERE invited_user=49 )
UNION
SELECT e.id FROM event e WHERE e.isEventDeleted=FALSE AND e.isNeighborlyInvited=TRUE AND e.organized_by!=49 AND EXISTS (SELECT id FROM address a WHERE latitude!='' AND longitude!='' AND e.event_address=id AND IFNULL(( 3959 * ACOS( COS( RADIANS(22.6979425) ) * COS( RADIANS( latitude ) ) * COS( RADIANS( longitude ) - RADIANS(75.8597305) ) + SIN( RADIANS(22.6979425) ) * SIN( RADIANS( latitude ) ))),0)<100) AND NOT EXISTS (SELECT eventid FROM event_interest WHERE e.id =eventid AND approvalStatus!='InterestExpressed' AND interested_user=49) AND NOT EXISTS (SELECT eventid FROM event_invite WHERE eventid=e.id AND invited_user=49 )
ORDER BY e.id,((SELECT AVG(avgr.abc) AS VALUE FROM ( SELECT YEAR(NOW())-YEAR(dob) AS abc FROM user_detail WHERE userid=(SELECT interested_user FROM event_interest WHERE eventid=477 AND approvalStatus='Approve') UNION SELECT YEAR(NOW())-YEAR(dob) AS abc FROM user_detail WHERE userid=(SELECT invited_user FROM event_invite WHERE eventid=477 AND acceptance='Accept')) AS avgr)+(SELECT IFNULL(( 3959 * ACOS( COS( RADIANS(22.6979425) ) * COS( RADIANS( latitude ) ) * COS( RADIANS( longitude ) - RADIANS(75.8597305) ) + SIN( RADIANS(22.6979425) ) * SIN( RADIANS( latitude ) ))),0) FROM address a WHERE latitude!='' AND longitude!='' AND e.event_address=id));
This one is giving me error 1054 unknown column e.id in order clause.
I was checking on google every where its naming mistake, but i am not getting how it is naming mistake please let me know if i am doing any thing wrong.
Try this by removing the e from ORDER BY clause since you are using the UNION statement:
ORDER BY id
instead of
ORDER BY e.id
Your original question has been answered. e.id is no more available after UNION. Only id is.
However there is some things I would like to add:
If I am not mistaken, the only difference between the first statement and the second is that in the first statement you require e.startdatetime to be less than NOW(). So the first statement retrieves a sub set of what the second statement retrieves. So you can remove the first statement completely including the union clause.
You select only from table event. You order first by id. An id should be unique for a table. So the rest of the order by clause is just void; it won't change the order at all.
In a comment to the first answer you got, you remark that you would like to order by a column, but you dont want to show it in your results. To achieve this do the following:
.
select a, b, c
from
(
select a, b, c, d
... -- complete query here
)
order by d;
I have a table of categories and a table of items.
Each item has latitude and longitude to allow me to search by distance.
What I want to do is show each category and how many items are in that category, within a distance chosen.
E.g. Show all TVs in Electronics category within 1 mile of my own latitude and longitude.
Here's what I'm trying but I cannot have two columns within an alias, obviously, and am wondering if there is a better way to do this?
Here is a SQL fiddle
Here's the query:
SELECT *, ( SELECT count(*),( 3959 * acos( cos( radians(52.993252) )
* cos( radians( latitude ) )
* cos( radians( longitude ) - radians(-0.412470) )
+ sin( radians(52.993252) )
* sin( radians( latitude ) ) ) ) AS distance
FROM items
WHERE category = category_id group by item_id
HAVING distance < 1 ) AS howmanyCat,
( SELECT name FROM categories WHERE category_id = c.parent ) AS parname
FROM categories c ORDER BY category_id, parent
First, start with the distance calculation for each item, then join in the category information and aggregate and filter
select c.*, count(i.item_id) as numitems
from category c left outer join
(SELECT i.*, ( 3959 * acos( cos( radians(52.993252) ) * cos( radians( latitude ) )
* cos( radians( longitude ) - radians(-0.412470) ) + sin( radians(52.993252) )
* sin( radians( latitude ) ) )
) AS distance
FROM items i
) i
on c.category_id = i.category_id and distance < 1
group by category_id;
Is this what you're looking for:
SELECT categories.name, count(items.item_id) as cnt
FROM items
JOIN categories
ON categories.category_id=items.category
WHERE ( 3959 * acos( cos( radians(52.993252) )
* cos( radians( latitude ) )
* cos( radians( longitude ) - radians(-0.412470) )
+ sin( radians(52.993252) )
* sin( radians( latitude ) ) ) ) < 1
GROUP BY categories.category_id;
this gives:
Tvs | 1
You can put the expression for computing the distance inside a nested SELECT, and then join the results to the categories table, like this:
SELECT COUNT(*), cc.name FROM (
SELECT
i.item_id
, c.category_id
, ( 3959 * acos( cos( radians(52.993252) )
* cos( radians( latitude ) )
* cos( radians( longitude ) - radians(-0.412470) )
+ sin( radians(52.993252) )
* sin( radians( latitude ) ) ) ) AS distance
FROM items i
JOIN categories c ON c.category_id = i.category
) raw
JOIN categories cc ON raw.category_id = cc.category_id AND raw.distance < 1
GROUP BY cc.name
The nested query pairs up items and categories, and adds the calculated distance column. The outer query then filters the rows by distance, and groups them by category to produce the desired output:
COUNT(*) NAME
-------- ----
1 TVs
Demo on sqlfiddle.
i have two sql queries those are following
1) SELECT a.* FROM modzzz_listing_main as a LEFT JOIN modzzz_listing_rating as b ON a.id=b.gal_id WHERE LTRIM(a.city) = 'Houston' AND a.state = 'TX' AND a.tags LIKE '%Barber Shop%' ORDER BY b.gal_rating_sum DESC LIMIT 0 ,10
2) SELECT zip_code ,( 3959 * acos( cos( radians('41.97734070') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('-70.97234344') ) + sin( radians('41.97734070') ) * sin( radians( latitude ) ) ) ) AS distance FROM city_finder WHERE latitude IS NOT NULL AND longitude IS NOT NULL HAVING distance < 20 ORDER BY distance ASC
how can i combine this two queries by the condition `
modzzz_listing_main.zip=city_finder.zip_code
` .i am totally confused..please any one help me..
to see the join easier:
select * from
(
SELECT a.* FROM modzzz_listing_main as a LEFT JOIN modzzz_listing_rating as b ON a.id=b.gal_id WHERE LTRIM(a.city) = 'Houston' AND a.state = 'TX' AND a.tags LIKE '%Barber Shop%' ORDER BY b.gal_rating_sum DESC LIMIT 0 ,10
) queryA
left join
(
SELECT zip_code ,( 3959 * acos( cos( radians('41.97734070') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('-70.97234344') ) + sin( radians('41.97734070') ) * sin( radians( latitude ) ) ) ) AS distance FROM city_finder WHERE latitude IS NOT NULL AND longitude IS NOT NULL HAVING distance < 20 ORDER BY distance ASC
) queryB
on queryA.zip=queryB.zip_code
proper formatting
SELECT *
FROM
( SELECT a.*
FROM modzzz_listing_main AS a
LEFT JOIN modzzz_listing_rating AS b ON a.id=b.gal_id
WHERE LTRIM(a.city) = 'Houston'
AND a.state = 'TX'
AND a.tags LIKE '%Barber Shop%'
ORDER BY b.gal_rating_sum DESC LIMIT 0 ,
10 ) queryA
LEFT JOIN
( SELECT zip_code ,
(3959 * acos(cos(radians('41.97734070')) * cos(radians(latitude)) * cos(radians(longitude) - radians('-70.97234344')) + sin(radians('41.97734070')) * sin(radians(latitude)))) AS distance
FROM city_finder
WHERE latitude IS NOT NULL
AND longitude IS NOT NULL HAVING distance < 20
ORDER BY distance ASC ) queryB ON queryA.zip=queryB.zip_code
I'm trying to get subjects with the highest average rating within a radius by averaging the comment rating column.
I've tried something along the lines of:
select avg(`rating`) AS `avgrate` , subject.*,
, ( 3959 * acos( cos( radians(22.28639) ) * cos( radians( lat ) ) * cos( radians( subject.long ) - radians(114.1491) ) + sin( radians(22.28639) ) * sin( radians( lat ) ) ) )
AS distance
from `subject_review`
JOIN `subject`
ON subject_review.subject_id = subject.id
group by `subject_id`
HAVING distance <= 100
order by `avgrate` DESC
LIMIT 10
This SQL works fine, I just need to add the radius filter,
select avg(`rating`) AS `avgrate` ,subject_review.id , subject.*
from `subject_review`
JOIN `subject`
ON subject_review.subject_id = subject.id
group by `subject_id`
order by `avgrate` DESC
LIMIT 10