2

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`, 
    ( 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?

6
  • 1
    Are you missing single quotes around accommodation or is it a valid column name? Commented Aug 21, 2011 at 10:15
  • 6
    The first step to solving a problem is reading the error message. Commented Aug 21, 2011 at 10:15
  • accommodation is the value and category is the column name Commented Aug 21, 2011 at 10:16
  • 2
    Is 'accommodation' a db field or a string? If it's a string it should be wrapped in single quotes. Commented Aug 21, 2011 at 10:16
  • Try reading the error message again. Commented Aug 21, 2011 at 10:16

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I thought I knew SQL but what sense has it to compare 2 unequal strings for equality?
how to compare strings like above in having clause?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.