php - 避免基于另一个列值的列值的 SQL INSERT 重复
问题描述
不确定问题是否很清楚,因此欢迎提出任何建议。
我有下表:
Table1
----------
ID fk_soc fk_product value
1 365 20 654896
2 365 21 654897
3 365 25 654898
4 820 20 114352
5 820 21 114381
6 820 25 114382
如您所见,在第二列中,我可以fk_product
为不同的客户 ID 设置相同的号码。但是如何避免fk_product
同一客户的重复记录?即避免
Table1
----------
ID fk_soc fk_product value
1 365 20 654896
2 365 20 whatever_here
如果我设置fk_product
为唯一,我将无法为其他客户插入记录。记录使用普通 SQL INSERT INTO 插入并来自 HTML 表单:
INSERT INTO Table1 (fk_soc, fk_distributor_product_code, fk_product)
VALUES (
' . $this->db->escape($this->fk_line) . ',
' . $this->db->escape($this->value) . ',
' . $this->db->escape($this->fk_product) . '
)
我试过使用:
INSERT INTO Table1 (fk_soc, fk_distributor_product_code, fk_product)
VALUES (
' . $this->db->escape($this->fk_line) . ',
' . $this->db->escape($this->value) . ',
' . $this->db->escape($this->fk_product) . '
)
WHERE NOT EXISTS (
SELECT ' . $this->db->escape($this->fk_product) . '
FROM table WHERE fk_soc= ' . $this->db->escape($this->fk_soc) . '
但是这种方式没有插入记录
解决方案
推荐阅读
- python - 在多类分割中使用 ImageDataGenerator 时训练 U-Net 的问题
- xslt - 谁能解释下面的 xsl 是如何工作的?
- python - 弹性搜索中的更新无法正常工作,有时会插入,有时会更新
- swift - 如何将 SFTranscriptionSegment 数据添加到 Firebase Firestore?
- html - Bootstrap 中的响应列,主列位于中心
- python - Python如何将字符串列表放入html + css字符串?
- postgresql - 使用 EF Core 5 查询 Postgres Json 字段
- firebase - 如何使用 Cloud Functions 创建新的 Firebase 实时数据库实例?
- onedrive - 阻止用户下载 OneDrive 中的特定文件
- node.js - 如何对集合中所有匹配文档中的特定 ID 执行 $lookup?