Order first table by status of second table

Discussion in 'MySQL' started by rogan4567, Nov 10, 2009.

  1. #1
    I have 2 tables, I'll call them categories and articles. I want to display the category names from category by the last_modified date of the article table.

    Here's an example of the setup

    Categories

    id | name | description

    Articles

    id | title | category_id | content | last_modified

    I've been interacting with MySQL for a while, but I only know what I've needed to do what I do, and this is well beyond what I know or even know how to look for.

    Basically, my goal is to display the category names DESC according to the last_modified time of the articles.

    Maybe something like this is the right direction?

    select distinct category_id from articles group by category_id order by last_modified desc

    then loop through the category table name?
     
    rogan4567, Nov 10, 2009 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    You need an INNER JOIN:

    SELECT Categories.name
    FROM Categories INNER JOIN Articles ON Categories.id = Articles.category_id
    GROUP BY Categories.name
    ORDER BY Max(Articles.last_modified) DESC;
    
    PHP:
     
    plog, Nov 10, 2009 IP
  3. rogan4567

    rogan4567 Active Member

    Messages:
    103
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    55
    #3
    Thank you -- this looks extremely useful.
     
    rogan4567, Nov 10, 2009 IP