开发者

Linking 3 queries together

I've been working on this all day, and I think my brain isn't functioning correctly right now.

I have 3 tables all "linked" together:

Boards table has:

- board_id

- board_name

Topic table has:

- topic_id

- topic_board_link (links to a board_id)

Post table has:

- post_time

- post_parent (links to a topic)

Essentially, I would like it to display the board, the last topic in the selected board, and the last post in that thread. So far, with my half functioning brain, I have (doesn't work, since max_topic isn't defined at the end, and it only returns 1 row):

SELECT 
    b.board_name,
    MAX(t.topic_id)开发者_如何转开发 as max_topic,
    MAX(p.post_id) as max_post
FROM board_list b
    INNER JOIN (topic_list t, post_list p)
    ON t.topic_board_link=b.board_id AND p.post_id=max_topic

Is there a way that I can do this without using tons of subqueries?


Add last_topic_id to the boards table and last_post_id to the topics table.

In this case you will have simple and really fast query that will return all what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜