0

I have a post table and a reply table. both tables have a created column which record when it is being created. and the post table have a last_reply_created and is updated whenever a reply made to a post with that post id.

first question: in the posts page, I want to display all posts that sorted by whichever last_reply_created or created in post table comes first.

second question: Can you design the post and reply table better regarding this problem?

My workaround is to insert last_reply_created whenever a post is created.

0

1 Answer 1

1

Your last_reply_created should be NULL until there is a reply and the creation time for a reply should always be more recent than the created value. This allows you to use coalesce like this:

select *
from your_table
order by coalesce(last_reply_created, created) desc

And presumably every row has a non-NULL created.

I don't see any problem with caching the last_reply_created value in your post table. If you're going to be using it a lot then it doesn't make much sense to keep computing it over and over again.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for that tip, I never noticed that change sir! Killed my answer as yours is sufficient :)
@David: I didn't mean to rain on your parade but you always have to keep an eye on those NULLs.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.