mysql查询使用连接显示数据?

时间:2014-04-12 09:29:46

标签: php mysql join

我有两个表,我想显示数据,这些数据不在表2中,但存在于表1中,列在数据之后。我想在表2中添加这些数据。 我应该使用哪个加入?请帮我解释代码

1 个答案:

答案 0 :(得分:1)

你根本不需要加入。您需要来自table1的数据,其中table2中没有条目存在。所以使用EXISTS子句。

select something 
from table1 
where not exists 
(
  select *
  from table2 
  where table2.somekey = table1.somekey
);

关于插入:

insert into table2 (column names)
select something 
from table1 
where not exists
(
  select *
  from table2 
  where table2.somekey = table1.somekey
);