sql删除使用内连接超过3个表
Posted
技术标签:
【中文标题】sql删除使用内连接超过3个表【英文标题】:Sql delete using inner join more than 3 tables 【发布时间】:2016-08-31 19:23:14 【问题描述】:我运行这个 sql 语句使用内部连接从数据库中删除,但是我得到了太多错误
错误
致命错误:带有消息的未捕获异常“PDOException” 'SQLSTATE[42000]: 语法错误或访问冲突:1064 你有一个 SQL 语法错误;检查与您对应的手册 MariaDB 服务器版本,用于在 'INNER JOIN 附近使用正确的语法 ipaddress_likes_map ON ipaddress_likes_map.postat = blog_post.BID INN' 在 C:\xampp\htdocs\codejail.net_inc\dbcontroller.php:57 的第 3 行 堆栈跟踪:#0 C:\xampp\htdocs\codejail.net_inc\dbcontroller.php(57): PDOStatement->execute() #1 C:\xampp\htdocs\codejail.net\user\deleteaccount.php(88): DBController->execute() #2 main 抛出 C:\xampp\htdocs\codejail.net_inc\dbcontroller.php 第 57 行
这是我的代码
<?php
if(isset($_POST['delete']))
$pass = $_POST['pasyske'];
$userkey = $_POST['username'];
$db_delete = new DBController();
$db_delete->prepare("DELETE
FROM blog_post
INNER JOIN ipaddress_likes_map ON ipaddress_likes_map.postat = blog_post.BID
INNER JOIN flagpost ON flagpost.postId = blog_post.BID
INNER JOIN postviewmap ON postviewmap.viewpostId = blog_post.BID
INNER JOIN replys ON replys.rid = blog_post.BID
INNER JOIN votepoint_map ON votepoint_map.postlike_id = blog_post.BID
WHERE blog_post.UserName = :alluserpost");
$db_delete->bind(":alluserpost", $userkey);
//$db_delete->bind(":password", $encrypt_password);
$db_delete->execute();
$pdeleted = $db_delete->getAll();
$db_delete->free();
if($pdeleted)
print_r($pdeleted);
?>
我确定我的数据库控制器没问题,因为当我运行单个删除语句时它工作得很好
【问题讨论】:
Delete with Join in mysql的可能重复 【参考方案1】:给每个表一个别名,并在单词DELETE
之后引用它。另外,请使用LEFT JOIN
而不是INNER JOIN
,因为我猜如果某些联接表没有匹配的行,您仍然希望删除工作事件。
db_delete->prepare("DELETE bp, i, f, p, r, v
FROM blog_post AS bp
LEFT JOIN ipaddress_likes_map AS i ON i.postat = bp.BID
LEFT JOIN flagpost AS f ON f.postId = bp.BID
LEFT JOIN postviewmap AS p ON p.viewpostId = bp.BID
LEFT JOIN replys AS r ON r.rid = bp.BID
LEFT JOIN votepoint_map AS v ON v.postlike_id = bp.BID
WHERE bp.UserName = :alluserpost");
【讨论】:
我收到了这个错误Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pb.BID' in 'on clause'' in C:\xampp\htdocs\codejail.net\_inc\dbcontroller.php:57 Stack trace: #0 C:\xampp\htdocs\codejail.net\_inc\dbcontroller.php(57): PDOStatement->execute() #1 C:\xampp\htdocs\codejail.net\user\deleteaccount.php(88): DBController->execute() #2 main thrown in C:\xampp\htdocs\codejail.net\_inc\dbcontroller.php on line 57
哎呀!错字。 pb
=> bp
。刚刚修好了。请再试一次。【参考方案2】:
在 DELETE 之后,您应该告诉从哪个表中删除行。试试这个查询:
$db_delete->prepare("DELETE blog_post
FROM blog_post
INNER JOIN ipaddress_likes_map ON ipaddress_likes_map.postat = blog_post.BID
INNER JOIN flagpost ON flagpost.postId = blog_post.BID
INNER JOIN postviewmap ON postviewmap.viewpostId = blog_post.BID
INNER JOIN replys ON replys.rid = blog_post.BID
INNER JOIN votepoint_map ON votepoint_map.postlike_id = blog_post.BID
WHERE blog_post.UserName = :alluserpost");
【讨论】:
没有错误,但表格没有删除@Andrej Ludinovskov以上是关于sql删除使用内连接超过3个表的主要内容,如果未能解决你的问题,请参考以下文章
mysql多个表之间的连接方式(内连接左连接右连接)delete删除表内数据的方法以及mysql索引