Forum Moderators: open

Message Too Old, No Replies

Query Question - Help With Join / Union

MySql query with join or union

         

xKillswitchx

12:41 am on Feb 9, 2007 (gmt 0)

10+ Year Member



Hello all. I am looking for a bit of help with a MySQL query. I believe I need a join or union here...

Anyways, I have two tables and am trying to pull data from them both.

news
id ¦ 1 auto +
cat_id ¦ 0 or 1
title ¦ title

news_categories
id ¦ 1 or 2
catname ¦ test cat 1 or test cat 2

This is for a news publishing application. I want to print off all news stories in the database and also be able to display their category. Would I use a join or union for this, and if so what would it look like?

Also, should I change news_categories id to cat_id? If so, that would be primary key correct? And cat_id of news would be just key as id is primary?

Thanks for any help you can offer. If needed, I can provide the snippet of script that has the query and prints the data off in alternating row colors.

rob_ot

12:56 am on Feb 9, 2007 (gmt 0)

10+ Year Member



Select n.*, c.catname from news n, news_categories c WHERE c.id = n.cat_id;

I'm no expert, but give that a try.

xKillswitchx

1:09 am on Feb 9, 2007 (gmt 0)

10+ Year Member



Yeah, that did it. Thanks a million. Now I just gotta figure out how you did that ;)

rob_ot

1:39 am on Feb 9, 2007 (gmt 0)

10+ Year Member



NP. The answer is the JOIN.

Google mysql join tutorial, that should get you gonig with them.