You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQLite多表关联插入时替换字符串通配符的实现方案咨询

在SQLite插入数据时替换通配符的解决方案

你可以利用SQLite的REPLACE()函数嵌套调用,实现多个通配符的依次替换。同时需要确保查询中关联plcs表,获取替换所需的path和device_shortcut值。

修改后的INSERT语句示例

INSERT INTO discovered_alarms_for_objects(
    alarm_type,
    name,
    input_tag,
    -- 根据你的表结构,添加其他需要替换通配符的字段
    description -- 假设该字段包含通配符
)
SELECT
    alarm_definitions.alarm_type,
    discovered_objects_with_alarms.id,
    -- 嵌套替换input_tag中的三个通配符
    REPLACE(
        REPLACE(
            REPLACE(
                alarm_definitions.input_tag,
                '[tag]', discovered_objects_with_alarms.tag
            ),
            '[path]', plcs.path
        ),
        '[device shortcut]', plcs.device_shortcut
    ),
    -- 对其他含通配符的字段执行相同替换逻辑
    REPLACE(
        REPLACE(
            REPLACE(
                alarm_definitions.description,
                '[tag]', discovered_objects_with_alarms.tag
            ),
            '[path]', plcs.path
        ),
        '[device shortcut]', plcs.device_shortcut
    )
FROM
    discovered_objects_with_alarms
    INNER JOIN objects_with_alarms ON objects_with_alarms.id = discovered_objects_with_alarms.object_with_alarm_id
    INNER JOIN alarms_for_objects ON alarms_for_objects.object_with_alarm_id = objects_with_alarms.id
    INNER JOIN alarm_definitions ON alarm_definitions.id = alarms_for_objects.alarm_definition_id
    -- 新增与plcs表的关联,根据实际外键调整连接条件
    INNER JOIN plcs ON plcs.id = discovered_objects_with_alarms.plc_id;

关键说明

  • 嵌套REPLACE(): SQLite没有一次性替换多模式的函数,因此需要嵌套调用,按顺序替换三个通配符,顺序不影响最终结果。
  • 关联plcs表: 必须加入plcs表的连接才能获取path和device_shortcut值,连接条件需根据你的实际表结构调整(示例中用discovered_objects_with_alarms.plc_id = plcs.id,替换为你的真实外键关联逻辑即可)。
  • 多字段处理: 如果alarm_definitions中有多个字段包含通配符,都需要按此替换逻辑处理,将每个字段的替换结果作为SELECT的列。

内容的提问来源于stack exchange,提问作者kubiej21

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 04:56:31