mysql query to show everything that does not join :) [closed]
Imagine two tables that both have a common ID field that joins two tables together.
Lets call them Table1 and Table2.
Table1 joins Table2 on the field ID.
How do I get all results on Table2 that has an ID that
guessing you need one of these
SELECT * FROM table1 JOIN LEFT table2 ON table1.id=table2.id WHERE table2.id IS NULL
or
SELECT * FROM table1 JOIN LEFT table2 ON table1.id=table2.id WHERE table2.id IS NOT NULL
select * from Table2 where ID not in (select ID from Table1)
or
select * from (select * from Table1 right join Table2 on Table1.ID = Table2.ID) where foo is NULL
in which foo
must be a column from Table1 that does not exist in Table2 and is never null in Table1.
EDIT: whoops, you can actually use ID as foo, as long as you qualify it with the table names:
select * from Table1.ID right join Table2.ID where Table1.ID is NULL
As explained here: http://www.codeproject.com/KB/database/Visual_SQL_Joins/Visual_SQL_JOINS_orig.jpg
Also interesting: http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
http://www.wellho.net/mouth/158_MySQL-LEFT-JOIN-and-RIGHT-JOIN-INNER-JOIN-and-OUTER-JOIN.html is that you want?!
mysql> select name, phone, selling
from Table2 join Table1
on Table2.pid = Table1.pid;
Or you want:
mysql> select *
from Table2
where Table2.pid is not null;
精彩评论