Jump to content

How to ...


icez

Recommended Posts

Hey,

 

I'm wondering how can I achieve the following using a simple query ( if it is possible ... )

 

I have 2 tables :

 

News

- id

- cat_id

- ...

 

Categories

- id

- title

 

using innodb I added a foreign key to news.id linked to categories.id

 

But I'm wondering, if a have a query similar too :

 

SELECT id, cat_id FROM news ....

 

how can I transform the cat_id to the actual title?

 

I don't know if I explained myself correctly, anyway,

Thank you guys, and excuse me for my english.

Link to comment
Share on other sites

First of all, the foreign key should be between news.cat_id and categories.id. news.id doesn't enter into the picture.

 

To get information from both tables you use a JOIN. A simple syntax is:

SELECT the fields you want from either table
FROM the primary table
JOIN the other table ON some condition

SELECT news.id, categories.title AS category
FROM news
JOIN categories ON news.cat_id = categories.id

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.