Check if one row exists in two tables

odorf

I have two tables:

friends:

accountId | friendId
--------------------
       A1 |      B15
       A1 |      C34
       A1 |      D48

and followers

accountId | followerId
----------------------
       A1 |        B15
       A1 |        C34

I try to run a MYSQL query that will return a "friendId" that does not exist in "followerId" column in the other table. Basically i try to find, which rows from the "friends" table, do not exist in the "followers" table.

I have tried:

SELECT *
FROM friends AS f
LEFT JOIN followers AS l
ON f.accountId = l.accountId
WHERE l.accountId IS NULL

but it return nothing. No result. Just null. I have checked the validity of the tables manually, and it does actually contain such entries. Any ideas?

Lukasz Szozda

You could use:

SELECT *
FROM friends AS f
LEFT JOIN followers AS l
  ON f.accountId = l.accountId
 AND f.friend_id = l.follower_id   -- additional condition
WHERE l.accountId IS NULL;

Output:

accountId | friendId | accountId | follower_id
       A1 |      D48 | NULL      | NULL

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Comparing two tables in laravel to check if a row exists in the tables

How can I check if row exists in multiple tables with one query?

For each row check if value in one column exists in two other columns

Azure Easy Tables - Check if row exists

MigraDoc Two Tables in one row

Select one row from multiple tables that only exists in one of the tables

Check there there exists only one null in each row, between two columns MySql (version 8.0.13)

Oracle - Check if row exists before deleting several rows in other tables

Optimizing query for Link two tables on one row

Combining two mysql tables into one row

How to check if the given username in the register page exists in some of the two tables

Query mySQL to check if user already exists in two tables

MySQL check if value already exists between two tables

Pandas dataframe check if a value exists in multiple columns for one row

Check if row exists in DataTable?

Check if a row exists in pandas

Check if row exists, Laravel

Check if row exists in database

Check if row exists in an xml

Check row if value exists

Transformation - Putting information that exists in two columns in one row

SQL - return differences between rows (in two different tables) but ONLY if row ID exists in both tables

sql server (Two tables and I want to display only one row)

Join two tables and return only one row from second table

how to select distinct values from two tables limit to one row

SQL Select data from two tables, one row -> multiple rows

fetchAll only fetches one row from two joined tables

SELECT JOIN two tables with multible rows in one row

how to insert into two tables; one will insert 1 row and the other one will insert multiple rows, the two tables has one column with the same value

TOP Ranking

HotTag

Archive