开发者

Mysql - Show unrelated results between two tables from table 1

I have to 2 tables:

table A:

pid|filename|date
-----------------
1  |file1   |2009
2  |file2   |200开发者_运维技巧9
3  |file3   |2009
4  |file4   |2009

table B:

pid|filename
------------
1  |filex1
1  |filex2
2  |filex3
2  |filex4

the result I want to get is:

pid|filename|date
-----------------
3  |file3   |2009
4  |file4   |2009

thats mean it should show me only the rows of table A that don't have a relation with the second table.

I started with this code, but it did not work!

$do_q=mysql_query("SELECT *
  FROM A LEFT JOIN B
         ON (A.pid <> B.pid)");
    while($row = mysql_fetch_array($do_q)){
        echo $row['pid'];
        echo $row['filename'];
        echo $row['date'];
    }

Thank you.


SELECT A.*
    FROM A
        LEFT JOIN B
            ON A.pid = B.pid
    WHERE b.pid IS NULL


I think this should work for you:

SELECT * 
FROM A
WHERE pid NOT IN (SELECT pid from B)


SELECT
 *
FROM tableA A
WHERE(NOT EXISTS(SELECT 1 FROM tableB B WHERE B.pid = A.pid))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜