sql >> Database teknologi >  >> RDS >> Mysql

MySQL - hvordan man viser det seneste emne pr. tråd

Ok, jeg kom selv med løsningen. Jeg brugte en afhængig underforespørgsel til at løse. Dette er, hvad jeg har i resultatet:

        SELECT
             item_discuss_threads.item_discuss_thread_id
             , item_discuss_threads.item_discuss_thread_datetime
             , item_discuss_threads.item_discuss_thread_title
             , latest_posts.item_discuss_post_title
             , latest_posts.item_discuss_post_datetime
             , COUNT(item_discuss_posts.item_discuss_post_id) AS nb_posts
        FROM
             item_discuss_threads
        INNER JOIN item_discuss_posts
             ON item_discuss_threads.item_discuss_thread_id=item_discuss_posts.item_discuss_thread_id
        INNER JOIN item_discuss_posts AS latest_posts
             ON latest_posts.item_discuss_thread_id=item_discuss_threads.item_discuss_thread_id
        WHERE
             (
                  SELECT
                        item_discuss_post_id
                  FROM
                        item_discuss_posts AS p
                  WHERE
                        p.item_discuss_thread_id=item_discuss_posts.item_discuss_thread_id
                  ORDER BY
                        item_discuss_post_datetime DESC
                  LIMIT
                       1
             )=latest_posts.item_discuss_post_id
        GROUP BY
             item_discuss_threads.item_discuss_thread_id
        ORDER BY
            latest_posts.item_discuss_post_datetime DESC


  1. Beregn tidsforskellen mellem to rækker

  2. Hvordan sender man vilkårlige parametre til Oracle trigger?

  3. Lagring af UTF-16/Unicode-data i SQL Server

  4. Sådan opretter du en tabel i MySQL