我想用如下语句删除查询结果,但是报错
错误如下:#1093 - You can't specify target table 'jo_sourcedata' for update in FROM clausedelete from sourcedata where exists(select * from sourcedata js where js.item_type=2 and not exists (select * from person jp where jp.per_id = js.item_id ))
我又想到用,如下的语句,但是也出现语法错误delete from sourcedata where id in (select js.id from sourcedata js where js.item_type=2 and not exists (select * from person jp where jp.per_id = js.item_id)
请帮忙看看用什么语句才能删除查询出来的结果,查询出来的结果有(10万多条),我想在phpmyadmin的sql里运行。谢谢。

解决方案 »

  1.   

    试试下面的delete from sourcedata where exists(select * from (select * from sourcedata) js where js.item_type=2 and not exists (select * from person jp where jp.per_id = js.item_id))
      

  2.   

    我测试数据库的信息sourcedata的数据全部删除了,
      

  3.   

    我怎么觉得你这个删除要删就是删全集呢? exists成立了,那么全部就删了。
      

  4.   

    那有什么好的方法只删除查询出来的。select * from (select * from sourcedata) js where js.item_type=2 and not exists (select * from person jp where jp.per_id = js.item_id
      

  5.   

    select * from sourcedata js where js.item_type=2 and not exists (select 1 from person  where per_id = js.item_id)
      

  6.   

    先备好好啊。没测试,也不知道对不对。delete * from sourcedata js where js.item_type=2 and not exists (select 1 from person  where per_id = js.item_id)
      

  7.   

    描述一下,
    有两张表:a ,b
    a 与 b通过 a.id ,b.aid 关联
    现在b表中存在很多多余的信息,a表没有b表的数据,于时通过如下查询出来了select * from b where b.item_type=2 and not exists (select * from a  where a.id = b.aid )
    现在我想继续操作,删除查询出的数据,该如何操作啊。谢谢。。不知道描述的清楚吗?
      

  8.   


    出现的错误:
    #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* from sourcedata js where js.item_type=2 and not exists (select 1 from pe' at line 1
      

  9.   

    分开来写!
    1、保存查询结果到临时表
    2、关联临时表进行删除phpmyadmin 可以一次执行多条指令
    php 代码中也只需执行2次 mysql_query不能连在一起写的原因是:你的删除条件是与可能被删除的记录相关联的结果集,一旦其中某条记录被删除了,整个结果集就无效了,必须重新产生。虽然理论上可以实现,但实际上行不通
    试想一下删除一万条记录的同时要做一万次查询,是个什么局面
      

  10.   

    这样可以删除~~ 应该不会出什么问题吧。我试了OKdelete from sourcedata where sourcedata.item_type=2 and not exists (select * from person  where person.per_id = sourcedata.item_id) limit 7000