Jump to content

Group By Ordering Help


greenday

Recommended Posts

Hi, I am getting results from 2 tables using the group by method. The reason for 2 tables is one contains all the news details and categoryid, and the other contains the categoryid and categoryid title.

 

Here is the code

 

SELECT articles.articlesummary, articles.articleid, articles.articlecategoryid, articlecategory.articlecategoryname
FROM articles, articlecategory
WHERE articles.articlecategoryid = articlecategory.articlecategoryid
GROUP BY articlecategory.articlecategoryname
ORDER BY articles.articlecategoryid ASC
LIMIT 0 , 30 

 

Works great, just as i want, except that its getting the first(oldest) articleid listing from the table for each article. I want the last(newest) articleid listing for each. Any help please?

 

Thanks.

Link to comment
Share on other sites

thanks, but that doesnt seem to work, even when i group by articles.articlecategoryid as suggested.

 

I think the problem is that any ordering happens after the results have been got, so its only ordering the same results differantly. Where as I am needing to get differant results in the first place.

 

Maybe my topic title was wrong. Any other ideas?

Link to comment
Share on other sites

Ah, I see. Well, since you are not using an aggregate, I doubt you need the group by clause. You see, it automatically sorts your results on that field unless you add "ORDER BY NULL" to it. And yes, it does this before ORDER BY is evaluated.

 

So, my suggestions is to remove the group by clause and instead:

 

ORDER BY articlecategory.articlecategoryname, articles.articleid DESC

 

 

 

Link to comment
Share on other sites

Ok, so you're looking to select the "latest" (which you define how? datetime? id?) article with it's category name?

Sounds a bit more like this: (until you give me full table description)

SELECT a.articlesummary
      ,a.articleid
      ,ac.articlecategoryname
FROM articles AS a
JOIN articlecategory AS ac ON a.articlecategoryid = ac.articlecategoryid
WHERE id = (SELECT MAX(articleid) FROM articles)

 

Your DDL (data definition language) will help clear up any inconsitencies in the above...

Link to comment
Share on other sites

aschk, thanks alot for your help, your sql statement gives me 1 record exactaly how i need, by selecting the highest articleid gives me the latest.

 

However, I am looking to get the 'latest' article that is in each articlecategoriy.  My origional group by done this, but it used the first results 'oldest' and lowest articleid.

 

Any ideas? maybe i need to somehow combine the group by and the join? cheers.

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.