php - SQL语法错误

标签 php sql mysql database

这是我的查询:

$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'";

我收到此错误:

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

如果您需要更多意见,请告诉我...

这里出了什么问题?

谢谢

UDP日期:

只是一个问题:我是否还需要指定 a =“此表”和 b =“另一个表”,还是 MySql 通过此代码获取该值?

至于发布的新代码在哪里使用 FROM 和终止符分号,将无法工作并给出此错误:

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

更新2:

$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'";

当我 echo $query 时:(在本例中,fordon 是 $sql_table 变量。)

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'

再次感谢

最佳答案

您没有指定要从中删除的表。尝试:

$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'";

我还在第一个 DELETE 查询结束后添加了分号。如果您想同时运行这两个版本,则在运行第二个版本之前需要一个分隔符来终止第一个查询。

回复。您的问题编辑有关 MySQL“获取”表的问题 - 如果 ab 是此处的别名,那么不,MySQL 不会不知道 ab 是什么。您需要为表添加别名,或将 ab 替换为实际的表名称。

关于php - SQL语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2972796/

相关文章:

mysql - 插入唯一行而不更新旧行

php - 将mysql转换为mysqli时出错

php - 将 PHP while 循环代码的内容放入 DIV 中

java - 为什么 jOOQ 建议将生成的代码放在 "/target"下而不是 "/src"下?

sql - 单元测试 "order by id"可以吗?

mysql - 错误 1396 (HY000) : Operation DROP USER failed for 'user' @'localhost'

mysql函数返回一个表

php - 在Codeigniter中: How to get url if i clicked on the fetched data from database

php - 查询适用于 mysql,但不适用于 php sql 调用

sql - 如何对每个项目的所有行求和?