繁体   English   中英

MySQL将行添加到存在一个值但不存在另一个值的表

[英]MySQL Add row to table where one value exists but not another

我不确定是否要问正确的权限,但是如何为customer_group_id = 0的每个file_id添加一个新的行,其中customer group = 0。 基本上,每个file_id都应该具有每个customer_group_id,但是有些缺少第0组和第1组。

|-----------------------------------------|
|id    |file_id|store_id|customer_group_id|
|-----------------------------------------|
|16389 |8376   |0       |1                |
|44241 |8376   |0       |2                |
|19669 |8376   |0       |3                |
|23765 |8376   |0       |11               |
|27861 |8376   |0       |21               |
|31956 |8376   |0       |31               |
|36051 |8376   |0       |61               |
|40146 |8376   |0       |71               |
|-----------------------------------------|

您可以对缺少的行进行INSERT 就像是:

insert into my_table (file_id, store_id, customer_group_id)
select file_id, '0', '0'
from my_table
where file_id not in (
  select file_id
  from my_table
  where customer_group_id = 0
)

如果这些字段是唯一索引,则可以将插入忽略或替换放入...。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM