Lucas Penney
Lucas Penney

Reputation: 2674

Get relational data with MySQL join?

I've been unable to find a good reference on how to use joins to accomplish a certain goal in MySQL.

I have a pages table, a categories table, and a pagecategories table. The rough structure of each:

pages: 'id', 'name', 'content' categories: 'id', 'name' pagecategories: 'pageid', 'categoryid'

I want to be able to use a query to get the category of a page, as well as the pages in a certain category.

Upvotes: 1

Views: 30

Answers (1)

John Woo
John Woo

Reputation: 263723

SELECT  a.name, a.content,
        b.name as CategoryName
FROM    pages a
        INNER JOIN pagecategories b
            ON a.ID = b.pageID
        INNER JOIN categories c
            ON b.categoryID = c.ID
-- WHERE a.ID = IDHERE       -- <<== if you want to filter for a specific page

To further gain more knowledge about joins, kindly visit the link below:

Upvotes: 1

Related Questions