Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Exclude all rows from one table if match on another table

Tags:

sql

mysql

Sorry for the very fuzzy question but my problem is that I have three different tables. One table containing user information, one for the users posts and one for the users likes. I want to select data from the tables containing the users data and posts but only return a post that the user have not liked and not is posted by the user itself. I have tried to use different combinations of JOINS but with no success.

For example i want to select the rows for the user with id = 1.

Table users:
+----+----------+
| id | username |
+----+----------+
|  1 |        A |
|  2 |        B |
|  3 |        C | 
+----+----------+

Table posts:
+----+---------+
| id | user_id |
+----+---------+
|  1 |       1 |
|  2 |       1 |
|  3 |       2 | 
|  4 |       3 | 
|  5 |       2 | 
|  6 |       3 | 
+----+---------+

Table likes:
+----+---------+---------+
| id | post_id | user_id |
+----+---------+---------+
|  1 |       3 |       2 |
|  2 |       3 |       1 |
|  3 |       4 |       1 |
|  4 |       1 |       3 |
+----+---------+---------+

Result wantend:
+---------+----------+
| post_id | username |
+---------+----------+
|       5 |        B |
|       6 |        C |
+---------+----------+

A problem I run into is that my query also returns post_id: 3 because user_id: 2 have liked the post.

I hope that you understands my question.

Thanks in Advance! /Andreas

like image 563
user3195845 Avatar asked Feb 28 '14 01:02

user3195845


People also ask

How do you SELECT all records from one table that do not exist in another table in SQL Server?

How to Select All Records from One Table That Do Not Exist in Another Table in SQL? We can get the records in one table that doesn't exist in another table by using NOT IN or NOT EXISTS with the subqueries including the other table in the subqueries.

How do you exclude rows in query?

Use the relational operators != or <> to exclude rows in a WHERE clause. The following query assumes that you are selecting from an ANSI-compliant database; the statements specify the owner or login name of the creator of the customer table.

How do I SELECT all rows except one?

You have a few options: SELECT * FROM table WHERE id != 4; SELECT * FROM table WHERE NOT id = 4; SELECT * FROM table WHERE id <> 4; Also, considering perhaps sometime in the future you may want to add/remove id's to this list, perhaps another table listing id's which you don't want selectable would be a good idea.

How do I exclude something in MySQL?

To check records which are NULL, use IS NULL. However, to exclude any of the records, use the NOT IN clause. Use both of them in the same query.


3 Answers

I think your data model isn't quite right, if "liking" a post adds a use to the "posts" table.

However, to answer your original question, you can exclude "liked" posts this way:

SELECT p.post_id, p.user_id FROM 
    post p LEFT JOIN 
    likes l 
    ON p.post_id = l.post_id WHERE l.post_id IS NULL;
like image 125
Daniel Avatar answered Sep 23 '22 21:09

Daniel


Here is an approach to the query that uses not exists for the likes:

select p.id as post_id, u.username
from posts p join
     users u
     on p.user_id = u.id
where not exists (select 1
                  from likes l
                  where l.post_id = p.id and l.user_id = 1
                 ) and
      u.id <> 1;
like image 33
Gordon Linoff Avatar answered Sep 22 '22 21:09

Gordon Linoff


To find rows that don't have a match in another table, use a LEFT JOIN and then select the rows where the foreign key is NULL.

SELECT p.id as post_id, u.username
FROM posts p
LEFT JOIN likes l ON l.post_id = p.id AND l.user_id = 1
JOIN users u
WHERE u.id != 1 and l.post_id IS NULL
like image 27
Barmar Avatar answered Sep 23 '22 21:09

Barmar