删除2表的查询无效

时间:2011-08-01 01:18:16

标签: mysql

public function deleteCustomerFunc($CustID)
{   
    $sql = "DELETE customer, cargo
    FROM customer AS cust, cargo AS car
    WHERE cust.CustID = car.CustID
    AND CustID='$CustID'";

    $result = mysql_query($sql);
    return $result;
}

我有2张桌子:

1)客户-CustID -Primary key 2)货物 - id -primary key,CustID =外键

3 个答案:

答案 0 :(得分:4)

特定的错误消息会有所帮助。但是,我最初的猜测是你的AND子句不明确 - CustID可以引用cust或car表。虽然在这种情况下哪一个并不重要,但解释器不知道这一点,你需要指定其中一个。

public function deleteCustomerFunc($CustID)
{   
    $sql = "DELETE customer, cargo
    FROM customer AS cust, cargo AS car
    WHERE cust.CustID = car.CustID
    AND cust.CustID='$CustID'";

    $result = mysql_query($sql);
    return $result;
}

答案 1 :(得分:0)

首先,DELETE语法不包含字段:

DELETE FROM [tablename] WHERE [condition]

其次,你不能从一个表达式中的两个表中删除,一次只能删除一个。

答案 2 :(得分:0)

也许您应该考虑将其设置为级联删除。然后当您从第一个表中删除时,它将通过外键自动从第二个表中删除。

How do I use on delete cascade in mysql?