从php / mysql中的两个表中删除一条记录

时间:2012-09-13 13:09:21

标签: php mysql

我正在尝试使用以下代码从船表中删除“船”并在另一个表中删除相关资格:

DELETE FROM tbl_boat, tbl_qualifications 
WHERE tbl_boat.BT_ID = '$bt_id' AND tbl_boat.BT_ID = tbl_qualifications.BT_ID;

问题是我收到以下错误:

  

1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在第2行'WHERE tbl_boat.BT_ID = 113 AND tbl_boat.BT_ID = tbl_附近使用正确的语法。

感谢您的任何帮助。

6 个答案:

答案 0 :(得分:0)

您可能想尝试INNER JOIN。

DELETE FROM tbl_boat
INNER JOIN tbl_qualifications
WHERE tbl_boat.BT_ID = '$bt_id' AND tbl_boat.BT_ID = tbl_qualifications.BT_ID;

我没有对此进行测试,但我认为这样可行。

答案 1 :(得分:0)

你必须告诉MySQL这两个表是如何相关的:

DELETE tbl_boat, tbl_qualifications
FROM
    tbl_boat
    INNER JOIN tbl_qualifications USING (BT_ID)
WHERE
    tbl_boat.BT_ID = '$bt_id'

答案 2 :(得分:0)

您需要执行两次删除:

  

- 首先删除所有关联...
  DELETE FROM tbl_qualifications WHERE BT_ID ='$ bt_id';

     

- ...然后删除船
  DELETE FROM tbl_boat WHERE BT_ID ='$ bt_id';

答案 3 :(得分:0)

应该是

DELETE tbl_boat, tbl_qualifications FROM tbl_boat, tbl_qualifications 
WHERE tbl_boat.BT_ID = '$bt_id' AND tbl_boat.BT_ID = tbl_qualifications.BT_ID;

顺便看一下How to delete from multiple tables in MySQL?

答案 4 :(得分:0)

这肯定有用:

DELETE a.*, b.*
FROM tbl_boat AS a
INNER JOIN tbl_qualifications AS b
    ON b.BT_ID = a.BT_ID
WHERE tbl_boat.BT_ID = '$bt_id';

此外,如果BT_IDINTEGER类型,则从$bt_id左右删除引文。

答案 5 :(得分:0)

试试这个

DELETE uploadfeat,postfeeds,postcomment FROM uploadfeat INNER JOIN postfeeds INNER JOIN postcomment
WHERE uploadfeat.id=postfeeds.postID AND uploadfeat.id=postcomment.postID AND uploadfeat.id=23