MariaDB 10.3.38使用公共表表达式删除时出现语法错误

taor4pac  于 2023-04-11  发布在  其他
关注(0)|答案(1)|浏览(145)

我试图删除一些数据在我的表中使用CTE,但其显示语法错误.只有删除语句似乎不工作,如果我只使用选择查询而不是删除,它确实工作.
代码

With results AS
    (Select * from table1 where time<='04:38:24' limit 10),
    results2 AS
    (Select UserId from table2 where address in (select * from results)),
    results3 AS
    (Select id from table4 where username in (Select * from results2))
    Delete from table3 using table3 join results3 on table3.column_id=results3.id;

错误

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Delete from table3 using table3 join results3 on `n...' at line 13

注意事项
1.表4和表3具有外键引用。
1.当我在workbench中使用相同的代码时,它不会给予任何错误,错误只发生在MariaDB CLI上。
我尝试使用CTE来使用delete语句,但它显示了一个语法错误。代码应该从table3中删除数据,但由于错误而没有发生。

xqk2d5yq

xqk2d5yq1#

看起来你正在使用一个非标准的SQL,这个标准语法应该适用于mont数据库系统:

WITH results AS (
    SELECT * FROM table1 WHERE time <= '04:38:24' LIMIT 10
), results2 AS (
    SELECT UserId FROM table2 WHERE address IN (SELECT * FROM results)
), results3 AS (
    SELECT id FROM table4 WHERE username IN (SELECT * FROM results2)
)
DELETE FROM table3 WHERE column_id IN (SELECT id FROM results3);

如果表3具有对表4的外键引用-将ON DELETE CASCADE添加到表3中的外键约束定义中(在表3中引用表4中被删除的行的情况下获取相关行)

相关问题