1

行の最新の日付を取得するためにこのサブクエリを機能させる方法がわかりません。

SELECT Thread_Heading.*, Thread_Articles.*, Thread_ArticlesPost.* 
FROM Thread_Heading 
LEFT JOIN Thread_Articles 
    ON Thread_Articles.Thread_Article_Head_id=Thread_Heading.Thread_Head_id 
LEFT JOIN 
(
    SELECT Thread_ArticlesPost.* 
    FROM Thread_ArticlesPost 
    ORDER BY Thread_ArticlePost_DT DESC
) Thread_ArticlesPost 
    ON  Thread_ArticlesPost.Thread_ArticlePost_Article_id=Thread_Articles.Thread_Article_id  
WHERE Thread_Head_Level = '5' 
GROUP BY Thread_Heading.Thread_Head_id 
ORDER BY Thread_ArticlePost_DT DESC

各スレッド記事の最新の投稿で記事の投稿日を並べ替える必要があります。SQLとPHPにかなり慣れていないので、これを解決できません。助けていただければ幸いです。

T

ABLE: Thread_Heading | 
Thread_Head_id
Thread_Head_Name
Thread_Head_Type
Thread_Head_Creator
Thread_Head_Date
Thread_Head_Level

TABLE: Thread_Articles | 
Thread_Article_id
Thread_Article_Head_id
Thread_Article_Creator
Thread_Article_DT
Thread_Article_Level
Thread_Article_Type
Thread_Article_Title

TABLE: Thread_ArticlesPost | 
Thread_ArticlePost_id
Thread_ArticlePost_Head_id
Thread_ArticlePost_Article_id
Thread_ArticlePost_Creator
Thread_ArticlePost_DT
Thread_ArticlePost_Level
Thread_ArticlePost_Type
Thread_ArticlePost_Title
Thread_ArticlePost_Content

次のように日付を表​​示する必要があります。

Head Name | Article title | ORDER BY LATEST ArticlePost DT | ArticlePost Creator

左結合を使用した理由は、記事や記事の返信がない場合でも左のデータを取得するためでした。

助けに感謝します。

これを使用してデータを表示しました ブルーのおかげで

SELECT *
FROM
( SELECT th.Thread_Head_Name,
ta. , tp1.maxdate, tp2. FROM Thread_Heading th LEFT JOIN Thread_Articles ta ON th.Thread_Head_id = ta.Thread_Article_Head_id LEFT JOIN
( SELECT max(Thread_ArticlePost_DT) maxDate,
Thread_ArticlesPost.* FROM Thread_ArticlesPost GROUP BY Thread_ArticlePost_Article_id ) tp1 ON tp1.Thread_ArticlePost_Article_id=ta.Thread_Article_id
LEFT JOIN Thread_ArticlesPost tp2 ON tp1.Thread_ArticlePost_Article_id = tp2.Thread_ArticlePost_Article_id AND tp1.maxdate = tp2.Thread_ArticlePost_DT
WHERE th.Thread_Head_Level = '5' ORDER BY tp1.maxdate DESC) m GROUP BY Thread_Head_Name ORDER BY Thread_ArticlePost_DT DESC

4

2 に答える 2

3

完全なテーブル スキーマが表示されない場合は、次のようなことを行う必要があります。

SELECT th.*, ta.*, tp.* 
FROM Thread_Heading th
LEFT JOIN Thread_Articles ta
    ON th.Thread_Head_id = ta.Thread_Article_Head_id
LEFT JOIN 
(
    SELECT max(Thread_ArticlePost_DT) maxDate, Thread_ArticlesPost.* 
    FROM Thread_ArticlesPost 
    GROUP BY Thread_ArticlePost_Article_id
) Thread_ArticlesPost tp
    ON  tp.Thread_ArticlePost_Article_id=ta.Thread_Article_id  
WHERE Thread_Head_Level = '5' 
GROUP BY th.Thread_Head_id 
ORDER BY tp.maxdate DESC

あなたの編集に基づいて、以下はあなたが望むデータを返すはずです:

SELECT th.Thread_Head_Name, 
  ta.Thread_Article_Title,
  tp1.maxdate,
  tp2.Thread_ArticlePost_Creator
FROM Thread_Heading th
LEFT JOIN Thread_Articles ta
    ON th.Thread_Head_id = ta.Thread_Article_Head_id
LEFT JOIN 
(
    SELECT max(Thread_ArticlePost_DT) maxDate, 
      Thread_ArticlesPost.Thread_ArticlePost_Article_id
    FROM Thread_ArticlesPost 
    GROUP BY Thread_ArticlePost_Article_id
) tp1
  ON  tp1.Thread_ArticlePost_Article_id=ta.Thread_Article_id  
LEFT JOIN Thread_ArticlesPost tp2
  ON tp1.Thread_ArticlePost_Article_id = tp2.Thread_ArticlePost_Article_id
  AND tp1.maxdate = tp2.Thread_ArticlePost_DT 
WHERE th.Thread_Head_Level = '5' 
ORDER BY tp1.maxdate DESC

編集#2、あなたのコメントに基づいて、以下のクエリは残りの問題を解決するはずだと思います:

SELECT th.Thread_Head_id,
  th.Thread_Head_Name,
  ta.Thread_Article_Title,
  tp.Thread_ArticlePost_Creator,
  tap.MaxPostDate
FROM Thread_Heading th 
LEFT JOIN
(
  SELECT max(ta.Thread_Article_DT) MaxArticleDate, 
    ta.Thread_Article_Head_id,
    max(tp.Thread_ArticlePost_DT) MaxPostDate
  FROM Thread_Articles ta
  LEFT JOIN Thread_ArticlesPost tp
    ON ta.Thread_Article_id = tp.Thread_ArticlePost_Article_id
  GROUP BY Thread_Article_Head_id
) tap
  ON th.Thread_Head_id = tap.Thread_Article_Head_id 
LEFT JOIN Thread_Articles ta
  ON tap.Thread_Article_Head_id = ta.Thread_Article_Head_id
  AND tap.MaxArticleDate = ta.Thread_Article_DT
LEFT JOIN Thread_ArticlesPost tp
  ON tap.MaxPostDate = tp.Thread_ArticlePost_DT
WHERE th.Thread_Head_Level = '5'
ORDER BY MaxPostDate desc

デモで SQL Fiddleを参照してください

于 2012-08-23T22:16:38.570 に答える
0

GROUP BY最新の投稿に基づいて、投稿がサブクエリで既にグループ化されている側の外側に句を含める必要はありません。これを試して、

SELECT  a.*, b.*, c.* 
FROM    Thread_Heading a
            LEFT JOIN Thread_Articles b
                ON b.Thread_Article_Head_id = a.Thread_Head_id
            LEFT JOIN Thread_ArticlesPost c
                ON c.Thread_ArticlePost_Article_id = b.Thread_Article_id 
            LEFT JOIN 
            (
                SELECT      Thread_ArticlePost_Article_id, MAX(Thread_ArticlePost_DT) maxDate
                FROM        Thread_ArticlesPost 
                GROUP BY    Thread_ArticlePost_Article_id
            ) d ON  d.Thread_ArticlePost_Article_id = b.Thread_Article_id AND
                    c.Thread_ArticlePost_DT = d.maxDate
WHERE       Thread_Head_Level = '5' 
-- GROUP BY a.Thread_Head_id 
ORDER BY    Thread_ArticlePost_DT DESC
于 2012-08-23T22:30:42.683 に答える