【问题标题】:MySQL compare two tables and add missing valuesMySQL比较两个表并添加缺失值
【发布时间】:2017-08-28 04:55:57
【问题描述】:

我有一个表 ps_feature :

-------------
|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   |
--------------------------------

谢谢。

【问题讨论】:

标签: mysql


【解决方案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

【讨论】:

  • 很高兴它有帮助!如果此答案或任何答案解决了您的问题,请单击复选标记考虑accepting it。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。
猜你喜欢
  • 1970-01-01
  • 2020-08-08
  • 2023-04-04
  • 1970-01-01
  • 2021-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-28
相关资源
最近更新 更多