我有一个表名论坛, 表格看起来像这样;
id studentid description teacherid
1 abc123 hi zxc123
2 abc123 hello qwe321
我想删除一行, 我尝试的是,
<?php
require_once('Connections/dbcon.php');
mysql_select_db("school", $dbcon);
$student_id=$_GET['student_id'];
$delete=mysql_query("DELETE from forum WHERE student_id='$student_id'")or die(mysql_error());
echo "<script language='Javascript'>alert('The Forum Successfully Deleted!');
location.href='tviewforum.php'</script>";
?>
我想要论坛中的特定消息。如果一个学生做了 2 个论坛,我想删除其中的 1 个并保留另外 1 个。
最佳答案
当一个学生创建的论坛超过1个时,您可以保留最大id
您可以使用group by
和having
来获取最大id 并删除其他行。
delete T from Table1 T
join ( select max(id) as id , studentid
from Table1
where studentid ='abc123'
group by studentid
having count(*) >1
) NewT
on T.id <> NewT.id
AND T.studentid = NewT.studentid
关于php - 使用 where 子句删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27344961/