douchi0471 2010-06-04 09:15
浏览 98
已采纳

Sql语法错误

Here is my query:

$query="Delete b
Where Exists 
    (
        Select 1
        From a
        Where a.poster_password = '$pass'
        And a.ad_id = '$id'
        And a.classified_id = b.classified_id
    )
Delete a
Where a.poster_password = '$pass'
And a.ad_id = '$id'";

I get this error:

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 'Where Exists ( Select 1 From a Where a.poster_p' at line 2

If you need more input let me know...

Whats wrong here?

Thanks

UDPATE:

Just a Q: Do I need to specify also that a = "this table" and b = "another table" or does MySql get that by this code?

As for the new code posted where to use FROM and a terminator semicolon, wont work and give this error:

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 'Delete FROM a Where a.poster_password = 'xxxxxxxxxxxxxxxxxxxxx' at line 10

UPDATE2:

$query="Delete FROM $sql_table
        Where Exists 
        (
            Select 1
            From classified
            Where classified.poster_password = '$pass'
            And classified.ad_id = '$id'
            And classified.classified_id = $sql_table.classified_id
        );
        Delete FROM classified
        Where classified.poster_password = '$pass'
        And classified.ad_id = '$id'";

And when I echo $query: (fordon is in this case $sql_table variable.)

Delete FROM fordon 
Where Exists 
(  
     Select 1 
     From classified 
     Where classified.poster_password = 'xxxxx' 
     And classified.ad_id = 'motorbat_166250627' 
     And classified.classified_id = fordon.classified_id 
 ); 
 Delete FROM classified 
 Where classified.poster_password = 'xxxxx' 
 And classified.ad_id = 'motorbat_166250627'

Thanks again

  • 写回答

2条回答 默认 最新

  • dongmie3526 2010-06-04 09:21
    关注

    You're not specifying the tables to delete from. Try:

    $query="Delete FROM b
    Where Exists 
        (
            Select 1
            From a
            Where a.poster_password = '$pass'
            And a.ad_id = '$id'
            And a.classified_id = b.classified_id
        );
    Delete FROM a
    Where a.poster_password = '$pass'
    And a.ad_id = '$id'";
    

    I've also added in a semicolon after the end of the first DELETE query. If you want to run both at the same time, you'll need a separator to terminate the first query, before you run the second version.

    Re. your question edit about MySQL "getting" the tables - if a and b are aliases here, then no, MySQL doesn't know what a and b are. You'll need to alias the tables, or replace a and b with the actual table names.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?