如何在 sql 查询中使用别名删除 table?
How can i delete table using alises on sql query?
我得到了
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'as idab, b.id_skb as idab FROM skd a, skb b WHERE idab='5'' at line 1** when i try to delete data from id by 2 tables...
here it is,
<?php
include './../../koneksi.php';
$idab= $_GET['idab'];
if (mysqli_query($koneksi, "DELETE a.id_skd as idab, b.id_skb as idab FROM skd a, skb b WHERE idab='$idab' "));{
echo ('<script>
alert("Data Anda Berhasil Dihapus...!!!");
window.location="http://localhost/administrasi_kelurahan/pengguna/administrator/Riwayat_Notif.php";
</script>');
}
?>
当尝试从两个 table 中删除时,您应该使用一个事务来耦合两个单独的删除操作,并提供干净的回滚可能性。否则,您可能会在一个 table 中删除记录,而在另一个中未删除记录。
如果两个 table 通过引用完整性链接,则首先在引用 table 中删除它,然后在引用的 table.
中删除它
执行两个 DELETE
查询,而不是一个。使用事务使其成为原子。
$stmt1 = mysqli_prepare($koneski, "DELETE FROM skd WHERE idab = ?");
mysqli_stmt_bind_param($stmt1, "s", $idab);
$stmt2 = mysqli_prepare($koneski, "DELETE FROM skb WHERE idab = ?");
mysqli_stmt_bind_param($stmt2, "s", $idab);
mysqli_query("START TRANSACTION");
mysqli_stmt_execute($stmt1);
mysqli_stmt_execute($stmt2);
mysqli_query("COMMIT");
我还展示了如何使用准备好的语句来防止 SQL 注入。
除非 table 之间存在关系,例如外键,否则您不能在单个查询中执行此操作。在那种情况下,当您从父 table.
中删除时,您可能会使用 ON DELETE CASCADE
自动从子 table 中删除
如果数据相关,可以用JOIN
DELETE a, b
FROM skb AS a
JOIN skb_pengikut AS b ON a.id = b.id_skb
WHERE a.id = ?
但是如果你用 ON DELETE CASCADE
选项将 id_skb
声明为外键,那么你只需要做:
DELETE FROM skb
WHERE id = ?
它会自动从 id_pengikut
中删除。
我得到了
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'as idab, b.id_skb as idab FROM skd a, skb b WHERE idab='5'' at line 1** when i try to delete data from id by 2 tables... here it is,
<?php
include './../../koneksi.php';
$idab= $_GET['idab'];
if (mysqli_query($koneksi, "DELETE a.id_skd as idab, b.id_skb as idab FROM skd a, skb b WHERE idab='$idab' "));{
echo ('<script>
alert("Data Anda Berhasil Dihapus...!!!");
window.location="http://localhost/administrasi_kelurahan/pengguna/administrator/Riwayat_Notif.php";
</script>');
}
?>
当尝试从两个 table 中删除时,您应该使用一个事务来耦合两个单独的删除操作,并提供干净的回滚可能性。否则,您可能会在一个 table 中删除记录,而在另一个中未删除记录。 如果两个 table 通过引用完整性链接,则首先在引用 table 中删除它,然后在引用的 table.
中删除它执行两个 DELETE
查询,而不是一个。使用事务使其成为原子。
$stmt1 = mysqli_prepare($koneski, "DELETE FROM skd WHERE idab = ?");
mysqli_stmt_bind_param($stmt1, "s", $idab);
$stmt2 = mysqli_prepare($koneski, "DELETE FROM skb WHERE idab = ?");
mysqli_stmt_bind_param($stmt2, "s", $idab);
mysqli_query("START TRANSACTION");
mysqli_stmt_execute($stmt1);
mysqli_stmt_execute($stmt2);
mysqli_query("COMMIT");
我还展示了如何使用准备好的语句来防止 SQL 注入。
除非 table 之间存在关系,例如外键,否则您不能在单个查询中执行此操作。在那种情况下,当您从父 table.
中删除时,您可能会使用ON DELETE CASCADE
自动从子 table 中删除
如果数据相关,可以用JOIN
DELETE a, b
FROM skb AS a
JOIN skb_pengikut AS b ON a.id = b.id_skb
WHERE a.id = ?
但是如果你用 ON DELETE CASCADE
选项将 id_skb
声明为外键,那么你只需要做:
DELETE FROM skb
WHERE id = ?
它会自动从 id_pengikut
中删除。