2017-04-03 100 views
0

我有一个表ps_feature:MySQL的比较两个表,并添加缺少的值

------------- 
|id_feature | 
------------- 
|1   | 
|2   | 
|3   | 
|4   | 
|5   | 
|6   | 
------------- 

,并用表ps_feature_lang:

-------------------------------- 
|id_feature | id_lang | name | 
-------------------------------- 
|1   | 1  | text1 | 
|2   | 1  | text2 | 
|4   | 1  | text3 | 
|5   | 1  | text4 | 
-------------------------------- 

是否有一个MySQL脚本在phpMyAdmin运行比较两个表并在ps_feature_lang中添加缺少的值?

结果应该是:

-------------------------------- 
|id_feature | id_lang | name | 
-------------------------------- 
|1   | 1  | text1 | 
|2   | 1  | text2 | 
|3   | 1  | N/A | 
|4   | 1  | text3 | 
|5   | 1  | text4 | 
|6   | 1  | N/A | 
-------------------------------- 

感谢。

+0

可能的复制[SQL - 找到一个表中的记录不以另一种存在(http://stackoverflow.com/questions/367863/sql-find-records-from-one-table-which-dont-exist-in-another) – Shadow

回答

1

要完成一个单一的语言,你可以使用的left join

insert into ps_feature_lang 
select f.id_feature, 1, 'N/A' 
from ps_feature f 
left join 
     ps_feature_lang fl 
on  f.id_feature = fl.id_feature and 
     fl.id_lang = 1 
where fl.id_feature is null 
+0

它的工作原理非常完美! 谢谢 – Mirco

+0

很高兴帮助!如果这个或任何答案已解决您的问题,请点击复选标记考虑[接受它](http://meta.stackexchange.com/q/5234/179419)。这向更广泛的社区表明,您已经找到了解决方案,并为答复者和您自己提供了一些声誉。没有义务这样做。 –

+0

我接受。 谢谢 – Mirco