MySQL QUERY HAVING clause not working with WHERE clause
I'm having issues with having clause and where clause I'm getting below error:
#1054 - Unknown column 'accommodation' in 'where clause'
Below is the code:
SELECT
b.bar_id, b.bar_name, b.bar_image, b.bar_lat, b.bar_lng,
b.bar_address, b.bar_phone, b.bar_email, b.bar_open_hours, `category`,
(开发者_StackOverflow中文版 6371 * acos( cos( radians(-37.81829) ) * cos( radians( b.bar_lat ) ) * cos( radians( b.bar_lng ) - radians(144.9620) ) + sin( radians(-37.81829) ) * sin( radians( b.bar_lat ) ) ) ) AS distance
FROM
bars b
WHERE
`category` LIKE accommodation
GROUP BY
category
HAVING
distance < 500
ORDER BY
distance
I cannot figure out why I'm getting this error. Does anyone have any suggestions?
Do you have a column called accommodation
? Or should you be doing a string comparison, in which case it should be 'accommodation'
(surround with single quotes)?
Also you might as well make it
`category` = 'accommodation'
Since you don't have any wildcards (%
) in the string.
If accommodation is a string value, then you need to wrap it in single quotes. Better yet, there's no need to use LIKE for an exact string, you can use equals instead.
ie:
SELECT b.bar_id, b.bar_name, b.bar_image, b.bar_lat, b.bar_lng, b.bar_address, b.bar_phone, b.bar_email, b.bar_open_hours, `category`, ( 6371 * acos( cos( radians(-37.81829) ) * cos( radians( b.bar_lat ) ) * cos( radians( b.bar_lng ) - radians(144.9620) ) + sin( radians(-37.81829) ) * sin( radians( b.bar_lat ) ) ) ) AS distance FROM bars b WHERE `category` = 'accommodation' GROUP BY category HAVING distance < 500 ORDER BY distance
精彩评论