2017-04-03 79 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