dqn48247 2014-12-07 16:40
浏览 157
已采纳

使用where子句删除行

I have a table name forum, the table looks like this;

id      studentid    description    teacherid   
1        abc123         hi           zxc123
2        abc123        hello         qwe321

I want to delete a row, what I tried is,

<?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>";   
 ?>

I want the specific message in a forum. If a student make 2 forums, I want to remove 1 of them and keep the other 1.

  • 写回答

1条回答 默认 最新

  • douwu8251 2014-12-07 17:02
    关注

    you can retain maximum id when there are more than 1 forum created by a student

    you can use group by and having to get the maximum id and delete other rows.

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写