所以,我寫一個小論壇,我想列出以下獲取線程+用戶名最後的海報
- 線程話題
- 的誰啓動線程的用戶名
- 的當它開始
- 誰在線程寫帖子的最後的
- 日期的最後一個人的用戶名日期
我有三個表此
帳戶
+---------------+
| id | username |
|---------------+
| 1 | blargh |
| 2 | hest |
+---------------+
線程
+----+-------+------+---------+
| id | topic | user | thedate |
+----+-------+------+---------+
| 5 | Yarr | 1 | bleh |
+-------------------+---------+
帖子
+----+---------+------+---------+--------+
| id | content | user | thedate | thread |
+----+---------+------+---------+--------+
| 8 | aaaa | 1 | aadate | 5 |
+----+---------+------+---------+--------+
| 9 | bbbb | 2 | bbdate | 5 |
+----+---------+------+---------+--------+
我想要什麼:
+----+-------+----------+---------+--------------------+----------------+
| id | topic | username | thedate | last_post_username | last_post_date |
+----+-------+----------+---------+--------------------+----------------+
| 5 | Yarr | blarg | bleh | hest | bbdate |
+----+-------+----------+---------+--------------------+----------------+
這是我走到這一步:
SELECT
forum_threads.id AS id,
forum_threads.topic AS topic,
forum_threads.time AS time,
accounts.username AS username,
Max(forum_posts.id) AS latest_post_id,
forum_posts.`user` AS `user`,
forum_posts.timeposted AS last_post_time
FROM
((forum_threads
JOIN forum_posts ON ((forum_posts.thread = forum_threads.id)))
JOIN accounts ON ((forum_threads.`user` = accounts.id)))
我似乎無法獲得最後的海報的用戶名和時間後說
爲什麼不直接使用已經在網絡上使用多年的成熟論壇軟件? –
爲了它的樂趣。 – Softnux
您的表格和「您想要的」與您的查詢不匹配得很好,這使得爲您寫出一個好答案變得更加困難,但可能最簡單的方法就是添加一個where條件要求'forum_posts.timeposted'等於從forum_posts返回max'timeposted'的子查詢,其中'thread = forum_threads.id'。 – jswolf19