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
https://forums.phpfreaks.com/topic/238832-how-to/
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
https://forums.phpfreaks.com/topic/238832-how-to/#findComment-1227230
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.