我们如何从mysql数据库中的表中获取不匹配的值

时间:2012-07-12 10:06:55

标签: php mysql join

从mysql数据库表中获取数据时遇到问题。 我有两个表,如下图中的表-1和表-2。当pilotid在表-1中不等于1时,如何从table-2获取数据。

Mysql database tables

4 个答案:

答案 0 :(得分:2)

我不确定,如果我理解正确的话,但这会返回table-1的所有行,表2中没有匹配的条目。您可以找到NOT EXISTS here的相应文档。

SELECT * 
  FROM table-1 t1
  WHERE NOT EXISTS( SELECT * FROM table-2 t2 WHERE t1.`Venueid` = t2.`Venueid` )

答案 1 :(得分:2)

select a.venueid, a.name 
from table2 a, table-1 b 
where b.pilotid <> 1 and b.venueid = a.venueid;

答案 2 :(得分:1)

SELECT        Table_2.*
    FROM      Table_2
    LEFT JOIN Table_1
        ON    Table_2.Venueid = Table_1.Venueid
    WHERE     Table_1.Venueid != 1
        OR    Table_1.Venueid NOT IN(1, 13, 15);

答案 3 :(得分:1)

$sql = "select Venueid from Table1 where pilotid <> 1";
$data = mysql_query($sql);
while($row = mysql_fetch_assoc($data))
{
 $ids[] = $row['Venueid'];
}
$sql2 = "select * from Table2 where venueid IN(".implode(',', $ids).")";
$data2 = mysql_query(sql2);

//$data2 contains the result-set resource;