简体   繁体   English

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

[英]delete a record from two tables in php/mysql

I'm trying to delete a "boat" from boat table and associated qualifications in another table using the following code: 我正在尝试使用以下代码从船表和另一个表中的关联资格中删除“船”:

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

The problem is I'm receiving following error: 问题是我收到以下错误:

1064 - You have an error in your SQL syntax; 1064-您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE tbl_boat.BT_ID = 113 AND tbl_boat.BT_ID = tbl_ ' at line 2 . 在第2行的'WHERE tbl_boat.BT_ID = 113 AND tbl_boat.BT_ID = tbl_ ”附近,查看与您的MySQL服务器版本相对应的手册以使用正确的语法。

Appreciate any help on this. 感谢对此的任何帮助。

You may want to try INNER JOIN. 您可能想尝试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;

I haven't tested this, but I think this will work. 我尚未对此进行测试,但我认为这会起作用。

You have to tell MySQL how the two tables are related: 您必须告诉MySQL这两个表是如何关联的:

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

You need to perform two delete : 您需要执行两个删除操作:

-- DELETE all associations first ... -首先删除所有关联...
DELETE FROM tbl_qualifications WHERE BT_ID = '$bt_id'; 从tbl_qualifications删除,其中BT_ID ='$ bt_id';

-- ... then delete the boat -...然后删除船
DELETE FROM tbl_boat WHERE BT_ID = '$bt_id'; 从tbl_boat删除,其中BT_ID ='$ bt_id';

it should be 它应该是

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;

btw take a look at How to delete from multiple tables in MySQL? btw看看如何在MySQL中从多个表中删除?

This will definitely work: 这肯定会起作用:

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

Also, if the BT_ID is INTEGER type, then remove the quotation from around $bt_id . 另外,如果BT_IDINTEGER类型,则从$bt_id周围删除引号。

Try This 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM