目前,我有两个查询。第一个从一行中选择一列。然后第二个删除该行。由于两个查询都处理同一行,所以我想知道是否可以一次执行两个查询(以减少代码量)。
我看过SELECT then immediately DELETE mysql record并尝试了Whatever Kitchen's answer
这是我事先的代码(可以正常工作):
$stmt = $con->prepare("SELECT number FROM viewings WHERE username=:user");
$stmt->bindParam(':user', $user);
$stmt->execute();
$row = $stmt->fetch();
$result = $row['number'];
$stmt = $con->prepare("DELETE FROM viewings WHERE username=:user");
$stmt->bindParam(':user', $user);
$stmt->execute();
echo $result;
这是尝试答案后的代码:
$stmt = $con->prepare("DELETE FROM viewings WHERE username=:user IN (SELECT number FROM viewings WHERE username=:user LIMIT 1)");
$stmt->bindParam(':user', $user);
$stmt->execute();
$row = $stmt->fetch();
$result = $row['number'];
echo $result;
但是,我收到这些错误:
致命错误:消息未捕获的异常“ PDOException” 'SQLSTATE [42000]:语法错误或访问冲突:1235此版本 的MySQL尚不支持'LIMIT&IN / ALL / ANY / SOME子查询' 在/home//public_html/page.php:47
堆栈跟踪:
0 /home//public_html/page.php(47):PDOStatement-> execute()
第4行的/home//public_html/page.php中抛出了1个{main}
答案 0 :(得分:0)
您可以尝试使用EXISTS
条件:
DELETE FROM viewings WHERE EXISTS (SELECT * FROM viewings WHERE username=:user LIMIT 1)