在n8n的MySQL节点中,【Operation】参数使用【update】,用于匹配的列有两个,分别是【target_date】和【report_type】,如何配置MySQL节点呢

在n8n的MySQL节点中,【Operation】参数使用【update】,用于匹配的列有两个,分别是【target_date】和【report_type】,如何配置MySQL节点呢

下午好🌅🌅🌅
本答案参考通义千问
在 n8n 中,使用 MySQL 节点 的 update 操作 时,确实支持通过多个字段进行匹配(即 WHERE 条件中包含多个字段)。但你可能在配置过程中遇到了限制,这是因为 n8n 的 UI 表单默认只允许选择一个字段作为匹配条件,但这并不意味着不支持多字段匹配。
where 参数手动输入 SQL 条件语句虽然 n8n 的界面可能只允许你选择一个字段,但你可以直接在 where 字段中写入 SQL 语句,从而实现多字段匹配。
updateOperation 为 update。Table 名称your_table_name。Fields to update在 Fields to update 中填写你想要更新的字段和值,格式为:
field1=value1, field2=value2
Where 字段中输入多字段匹配条件在 Where 字段中,直接写 SQL 条件语句,例如:
target_date = {{ $input.$date }}, report_type = {{ $input.$type }}
这里使用了 n8n 的变量语法
{{ ... }},表示从输入数据中获取值。
假设你的数据结构是这样的:
{
"target_date": "2025-04-05",
"report_type": "monthly",
"new_value": "updated"
}
那么你在 n8n 的 MySQL 节点中应这样配置:
| 字段 | 值 | |------|----| | Operation | update | | Table | your_table_name | | Fields to update | new_value={{ $input.new_value }} | | Where | target_date = {{ $input.target_date }}, report_type = {{ $input.report_type }} |
target_date 和 report_type 必须与数据库中的字段名一致。{{ ... }} 是动态变量占位符,用于将数据传递给 SQL 查询。✅ 支持多个字段匹配,但需要手动输入 where 条件,而不是依赖 UI 默认的单字段选择器。
update。Fields to update 中设置要更新的字段和值。Where 字段中编写 SQL 条件语句,例如:
target_date = {{ $input.target_date }}, report_type = {{ $input.report_type }}
{{ $input.target_date }})。如果你使用的是 n8n 的代码节点(Code node),可以手动构造 SQL 语句,例如:
const sql = `
UPDATE your_table_name
SET new_value = '${inputData[0].new_value}'
WHERE target_date = '${inputData[0].target_date}'
AND report_type = '${inputData[0].report_type}';
`;
return [{ json: { sql } }];
⚠️ 注意:此方法不推荐用于生产环境,建议使用 MySQL 节点的参数化查询来防止 SQL 注入。
如有更多问题,欢迎继续提问!