I have two tables listed below:-
- football – It has 2267 unique records.
- football_assets – It has 2716 unique records.
Now I’m running following mysql query in phpmyadmin :-
select * from football_assets where pair in (SELECT DISTINCT pair FROM `football`);
The query returns only 2191 players but it should give 2267 records. I want to know why??
thanks in advance.
2
Answers
The results you are getting show that there are 2267-2191 = 76 records in football_assets whose pair cannot be found in football.
You can exhibit these records by changing the IN in your query to NOT IN :
First, the
select distinct
is redundant in the subquery. AWHERE
clause does not change the number of rows. So, you should write:Obviously, you have some pairs that are not in both tables. I strongly encourage you to use
not exists
orleft join
/where
to find them:Do not use
not in
. It will return no rows at all if even a single value ofpair
isNULL
infootball
. That is because of howNULL
s work in SQL. For this reason, I strongly discouragenot in
with subqueries.