2014-12-07 93 views
1

我有一個表名論壇, 表看起來像這樣;使用where子句刪除行

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.

+0

如果您想刪除'forum'表中的一行,請使用'forum.id'而不是'forum.studentid'。 – 2014-12-07 16:45:09

+0

你能幫助我通過使用這個查詢來查詢 – 2014-12-07 16:47:41

回答

0

可以保留最大的ID時,有一個學生創建

你可以使用超過1個論壇group byhaving獲取最大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 
+0

嗎,如果學生在表中有多於1個論壇,並且我想把所有這些都留在我的數據庫中,是否可能?並在以後刪除特定的論壇 – 2014-12-07 17:25:58

+0

它的工作!非常感謝!!!! – 2014-12-07 17:31:37