0

I am trying to pull all categories for a blog post when the blog is loaded. I have 2 tables for categories, one for the categories themselves and another that lists which categories are assigned to which blog post.

the MySQL command I have is

SELECT `category_name`, `category_slug` FROM `blog_categories`, `blog_post_categories` WHERE `blog_categories.category_id` = `blog_post_categories.category_id` AND `blog_post_categories.post_id` = 1

But I keep getting the following error

Unknown column 'blog_categories.category_id' in 'where clause'

The two tables are as such:

blog_categories has columns category_id, category_name and category_slug blog_post_categories has columns id, post_id and category_id

1
  • Do not use comma-join syntax. And you shouldn't need backticks on properly qualified columns (such as these are) Commented Apr 21, 2014 at 15:20

1 Answer 1

3

Your backticts are not in proper place should be as

SELECT `category_name`, 
`category_slug` 
FROM 
`blog_categories`, `blog_post_categories` 
WHERE `blog_categories`.`category_id` = `blog_post_categories`.`category_id` 
AND `blog_post_categories`.`post_id` = 1
Sign up to request clarification or add additional context in comments.

1 Comment

Abhik, thank you so much. Still a noob with sql so this helps alot. Will accept as soon as am able to.

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.