Treedot Treedot - 2 years ago 63
SQL Question

Use the same ID for two rows in two different tables

So, I'm building a message board type website. I've got top level posts, which are stored in a posts table, and comments to those posts, which are stored in a comments table.

When you make a post (thread or comment) it's ID increments, however right now the comments and the posts have different IDs that increment on their own. Like, there can be a post #65 and a comment #65.

I'd like to make it so they share the same ID. For example, if someone makes a post and it's #65, the next post, comment or thread, will be #66.

I tried googling around, but I can't think of how to word this question, if anyone can help I would really appreciate it!

Answer Source

Add a column to Your comments table, and name it - for example - reply_to - the column should contain the top post's ID to which the comment replies to... If You use innoDb don't forget to add a foreign key there: alter table comments add foreign key(reply_to) references posts(id); Good Luck!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download