DELETE FROM对执行SELECT的查询抛出SQL错误[1064] [42000]:conn = 5159

问题描述

尝试删除MySQL查询中特定数量的行,我可以使用以下命令SELECT删除想要删除的任何内容,以获得所需的结果:

select * from ns_cos ns where ns.created_at <>
    (select max(nsa.created_at) from ns_cos nsa
        where nsa.month_year = ns.month_year)

但是,当我尝试使用以下方法删除所选数据时:

delete from ns_cos ns where ns.created_at not exists
    (select max(nsa.created_at) from ns_cos nsa
        where nsa.month_year = ns.month_year)

我得到:

sql错误[1064] [42000]:(conn = 5159)您的sql语法有错误;请查看与您的MariaDB服务器版本相对应的手册,以获取正确的语法,以在第1行的'ns ns.created_at不存在的ns附近使用(从ns_cos nsa wh中选择max(nsa.created_at))

我在做什么错了?

解决方法

EXISTS中没有可能使用IN子句,但是您需要将表包含在单独的select中,以便mysql认为它是另一个表

delete from ns_cos ns 
where ns.created_at not IN (select max(nsa.created_at) from (SELECT * FROM ns_cos) nsa where nsa.month_year = ns.month_year)
,

您的直接问题是,并非所有MySQL版本都支持直接在 function traverse(obj,parameters){ obj.forEach((item,index)=>{ if( typeof item == 'string' ){ //do nothing } else if( !(item instanceof Array)){ Object.entries(item).forEach((item,index)=>{ if( item[1] instanceof Array){ traverse(item,parameters); }else{ if(item[1].startsWith('$p:')){ item[1]=parameters[item[1].split('$p:')[1]] //values dont get replaced for obvious reason console.log(item[1]) } } }) } else if( item instanceof Array){ traverse(item,parameters); } }) } traverse(criteria,parameters) console.log(criteria) 中对表进行别名。此外,尽管如此,您无法重新打开在delete from子句中删除from的表。

考虑使用from语法。

delete ... join