user2463787
user2463787

Reputation: 29

MySQL Query - five latest articles in a specific category

Alright so I’m stuck on this query, I have two tables one called Category and the other called Articles. From Articles I want to get ArticleID, Title, Preamble, and Date. And In the other table that is called Category that has MainCategory and Subcategory. Now what I want to achieve from category I would like to get the five latest articles and they should be from a specific MainCategory for example I have the MainCategory Concert and would like to get the five latest articles. Also they should include the Title Preamble Article ID and date from the specifick Article. Here is an Example of what I mean in table form Table category’s

ArticleID  MainCategory      Subcategory      Title       Preamble            Date              
1           Concert              POP        Rock Music     blalba          xxxx-xxx-xxx
5           Concert              Rock
6           Concert              Hip-Hop
12          Concert              Classic

I’m stuck with how the structure of the query should look like but this is how far I have gotten, thought I really believe that I’m missing something.

SELECT  ArticleID, Title, Preamble, and Date,  Subcategory
FROM  category, ArticleID
ORDER BY ArticleID DESC LIMIT 5 

Should I perhaps use myself of a Join to get the right information from the two different tables. Would appreciate if someone can throw me in the right direction

Upvotes: 0

Views: 78

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270391

Assuming you are interested in the category "Concert", you need both a join and a where clause:

SELECT a.ArticleID, a.Title, a.Preamble, a.Date,  c.Subcategory
FROM  Articles a join
      Category c
      on a.categoryid = c.categoryid
where MainCategory = 'Concer'
ORDER BY ArticleID DESC
LIMIT 5 

Upvotes: 1

Related Questions