Suppose a table named ‘users’, a table named ‘posts’ and a table named ‘ratings’, that contains the rating (either a ‘like’ or ‘dislike’) of each user towards each post.
create table ratings (
user_id int unsigned not null,
post_id int unsigned not null,
rating set('like','dislike') not null,
primary key (user_id, post_id)
);
Given a post X, I want to select all the posts Y such that no user who rated them both, liked them both. Furthermore, if possible, I would like to order those posts by the amount of common users who rated them, meaning that the post Y with most common ‘raters’ with X should appear first.
I’d copy what I’ve done so far, but I think none of it is worth it. Any help appreciated, thanks!
Try this fiddle. I’m not 100% sure I got what you wanted, but… check with your own data if it fits.
You can also remove the
WHEREand addX.post_id AS this_post_idinto theFROMto get this for all posts.