我试图在mysql的一个查询中从多个表中删除。它给了我以下错误:
错误:您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在'* FROM posts
INNER JOIN fields_data
附近使用正确的语法在第1行的posts.post_id = fields_data.post_id W'
<?php
include "connection.php";
$id=@$_GET['pid'];
$ssq="DELETE FROM `posts` INNER JOIN `fields_data` ON posts.post_id =
fields_data.post_id WHERE posts.post_id= '$id'";
$query=mysqli_query($con,$ssq);
if($query){
header("location:home.php?del=The post has been deleted successfully");
}
else{
echo "ERROR:".mysqli_error($con);
}
?>
答案 0 :(得分:2)
在DELETE
关键字后使用表格名称
$ssq="DELETE `posts` FROM `posts` INNER JOIN `fields_data` ON posts.post_id =
fields_data.post_id WHERE posts.post_id= '$id'";
使用别名来更好地理解
$ssq="DELETE p FROM `posts` as p INNER JOIN `fields_data` as fd ON p.post_id =
fd.post_id WHERE p.post_id= '$id'";
修改强>
如果要删除两个表中的记录,只需在DELETE
关键字后指定表名:
$ssq="DELETE `posts`,`fields_data` FROM `posts` INNER JOIN `fields_data` ON posts.post_id =
fields_data.post_id WHERE posts.post_id= '$id'";
答案 1 :(得分:2)
试试这个..
您经常在SELECT语句中使用INNER JOIN子句从表中选择其他表中具有相应记录的记录。为了更方便,MySQL还允许您使用带有DELETE语句的INNER JOIN子句来删除表中的记录以及其他表中的相应记录,例如,删除满足特定条件的T1和T2表中的记录,您使用以下语句:
DELETE T1, T2
FROM T1
INNER JOIN T2 ON T1.key = T2.key
WHERE condition
您的代码
$ssq="DELETE posts,fields_data FROM `posts` as posts INNER JOIN `fields_data`as fields_data ON posts.post_id =
fields_data.post_id WHERE posts.post_id= '$id'";