3 ms·
> Thanks, I had not thought JOINs possible on this - how would a query look? So this is for the structure with the two columns reference_id: '1234', r
by ralusek 8y ago
> Thanks, I had not thought JOINs possible on this - how would a query look?
So this is for the structure with the two columns
reference_id: '1234',
reference_table: 'posts'
reference_id: '4321',
reference_table: 'comments'
So let's say that this is the `likes` table that can like either `comments` or `posts`, here are some queries:
// Get comments and their respective likes
SELECT * from comments
JOIN likes ON comments.id = likes.reference_id;
// Get posts and their respective likes
SELECT * from posts
JOIN likes ON posts.id = likes.reference_id;
// Get likes and their referenced comments
SELECT * from likes
JOIN comments ON comments.id = likes.reference_id;
And so on.