This question has been asked before but never really answered (at least the way I want to do it). I have three tables in my database, threads, responses and votes. I need them to share the id field only. So I can do something like
SELECT * FROM threads AS t JOIN responses AS r JOIN votes AS v
WHERE id = 15
and will only retrieve a thread a response or (exclusive) a vote record with id = 15.
Is there a possible way to do this without creating an extra table? I’m not asking if it’s a good idea (which is probably not), but if it’s possible and how to do it.
Your select will throw an error, because the
idfield in the where clause is ambiguous. I suppose there is anidfield in all of these three tables.So even if the
idfield is “somehow shared” between these fields, you will have to make your where clause unambiguous:Furthermore, I suppose threads and responses have a many-to-one relationship which means there can be several response corresponding to a single thread. This means the
responsestable would have athread_idfield. Theidfield in theresponsestable could not have the same value as theidfield in the correspondingthreadrecord, since theidfield must be unique in both tables.The same logic goes to the relationship between
responsesandvotes(a single response would have many votes).Hence I conclude there is no possible way to share the
idfield between these three tables.