Jump to content

[SOLVED] JOIN, AS,


xstevey_bx

Recommended Posts

I am really new to mySQL and have been trying to get this working all day but I just cant get my head round the concepts im reading. I was wondering if someone would solve my problem so I have a way to relate the code to what im doing to help give me named examples rather than the table1 and table 2 once I have come across

 

I have a 'news' table

idcategory_idnews article

11article text

21article text

33article text

41article text

54article text

61article text

 

and a 'news_categories' table

 

idnamedescription

1Blogblog description

2Newsnews description

3Updatesupdates description

4Otherother description

 

I have all the information stored in a database and want to SELECT * FROM 'news' WHERE news.category_id = news_categories.id AND news.category_id = $_GET['category']???...

 

but when the values are put into an array I want a column name to be created 'category' and I want its value to be the 'name' column from 'news_categories' = 'category_id' for that particular articles id?

 

(SELECT name.news_categories AS category WHERE news.category_id = news_categories.id)??

 

Thanks for any help

Link to comment
Share on other sites

a join is fairly easy to think about when you look close at the syntax.  First when working with joins, we have to explicitly tell mysql which table to deal with.  So our select statement needs to be:

 

 

Select news.*, news_categories.* .....

 

or since we don't really care to use the id's:

 

Select

news.`news article`,

news_categories.name, news_categories.description  ....

 

This says our result 'table' will have those three columns (news, article. name and description).

 

Now we need to join the tables together.  Since news.category_id will (or should) always correspond to a entry in news_categories, we will just use a generic join:

 

Select

news.`news article`,

news_categories.name, news_categories.description

FROM news, news_categories

WHERE news.category_id = news_categories.id

 

Now we just need to limit the search to a particular category:

 

Select

news.`news article`,

news_categories.name, news_categories.description

FROM news, news_categories

WHERE news.category_id = news_categories.id

AND news.category_id = '$category'

 

Since you want to rename stuff, we need to use the AS statement.  You want the news_categories.name column to be called 'category'

 

Select

news.`news article`,

news_categories.name AS category, news_categories.description

FROM news, news_categories

WHERE news.category_id = news_categories.id

AND news.category_id = '$category'

Link to comment
Share on other sites

I have solved my problem :) It outputs as I like, I have added slight modifications to allow me to refine the news results and allow sorting.

 

$query  = " SELECT news.*, news_categories.name AS category

FROM news, news_categories

WHERE news.category_id = news_categories.id

AND news.category_id IN ($category)

ORDER BY $key $order

LIMIT $offset, $toplimit";

 

Is that a technically correct query to use?

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.