Jane Panda
Jane Panda

Reputation: 1671

MySQL select statement from two tables

A common scenario I imagine, but I can't seem to find the terminology to find this answer...

I have two tables, one referencing the other like so:

topics
------------
title
category_id

categories
------------
category_id
category_title

How would I write a query to select the title and category_title of a topic, instead of the id?

Upvotes: 1

Views: 975

Answers (3)

Hiren Dabhi
Hiren Dabhi

Reputation: 3713

Try this query

select t.title, c.category_title from topic t, categories c where t.category_id = c.category_id;

Upvotes: 2

Matt Phillips
Matt Phillips

Reputation: 11499

The terminology you are looking for is called a join.

select title,category_title from topics,categories where title.category_id = categories.category_id;

Upvotes: 2

Adriaan Stander
Adriaan Stander

Reputation: 166326

How about something like

SELECT  title, 
        category_title
FROM    topics t inner join
        categories c    ON t.category_id = c.category_id

Have a look at JOIN Syntax

Upvotes: 3

Related Questions