让我们说我有这些变量:
$post_id = 222;
$IsLoggIn =2;
我在五个不同的表中有相同的列“post_id”名称。 我想编写一条sql语句来检查是否存在列名为“post_id”的行,其值等于变量“$post_id”并删除该行
注意:除了“public_feed_table”之外,可能没有任何行post_id等于变量” $post_id" 所以sql语句应该在删除之前检查是否有一行。
我想要一个可以完成这项工作的 SQL 语句。
我尝试过的如下。请帮忙:
global $dbc_conn,
$public_feed_table,
$images_table,
$comments_table,
$rating_table,
$notification_table,
$IsLoggIn;
$sql = "DELETE
p,i,c,r,n
FROM
$public_feed_table p
LEFT JOIN
$images_table i,
$comments_table c,
$rating_table r,
$notification_table n
ON
i.post_id,
c.post_id,
r.post_id,
n.post_id=p.post_id
WHERE p.post_id='$post_id'
AND p.user_id='$IsLoggIn'
";
//query database
$query = mysqli_query($dbc_conn,$sql);
最佳答案
您加入语法已磨损。它必须是:
$sql = "DELETE p,i,c,r,n
FROM
$public_feed_table p
LEFT JOIN
$images_table i ON
p.post_id = i.post_id
LEFT JOIN
$comments_table c ON
p.post_id = c.post_id
LEFT JOIN
$rating_table r ON
p.post_id = r.post_id
LEFT JOIN
$notification_table n ON
p.post_id=n.post_id
WHERE p.post_id='$post_id'
AND p.user_id='$IsLoggIn'
";
关于mysql - 从两个或多个表中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40175405/