我有三个表:
student_t:id name
student_course_t:student_id,course_id
course_t:id name
我想实现这样的删除:比如说student_course_t中有数据1 1,我想把它删除……也就是删除多对多关系中中间表的单条数据的sql语句……

解决方案 »

  1.   

    使用简单的delete语句就可以了,你的中间表是用student_id和course_id做为主键
    delete from student_course_t  where student_id='1' and course_id='1'
    如果是删除student_t和这个表course_t中的数据先要删除student_course_t 对应的数据因为存在约束条件
      

  2.   

    不好意思……弄错了……可以了……但是如何用hql语言写呢?
      

  3.   

    写一个事务
    先删除主键表,再删除外键表至于hql语句,网上很多。照着写就是了
      

  4.   

    既然用到hibernate了,就直接一个对象,再在Dao里面this.getHibernateTemplate().delete(entity);不就行了。它是副表,删了没啥问题的,又不是删除course和student可能会出现违法参照完整性的事儿。
      

  5.   

    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
    corresponds to your MySQL server version for the right syntax to use near 'wher
     exists (select courseid from course_t c where sc.course_id = c.courseid an' at
    line 1
      

  6.   

    不要图片,CSDN没这功能,直接复制一下错误信息的文本就可以了。
      

  7.   

    'exists (select c.courseid from course_t c where sc.course_id = c.courseid它哪里知道courseid是属于c还是sc的???
      

  8.   

    mysql> delete from student_course_t sc where exists (select courseid from course
    _t c where sc.course_id = c.courseid and c.courseid = 1 );
    这是执行的语句
      

  9.   

    看你的hql咋个写的,
    或者直接用createSqlQuery()执行sql不就行啦
      

  10.   

    你直接delete from student_course_t sc where sc.course_id = 1不就行了
      

  11.   

    不行,提示Update queries only supported through HQL
      

  12.   

    不行提示Update queries only supported through HQL
      

  13.   

    public boolean delete(Integer studentid,Integer courseid) {
    final String sql = "delete from Course c where student_id = "+studentid+" and course_id = "+courseid;
    return (Boolean) this.getHibernateTemplate().execute(
    new HibernateCallback() {
    public Object doInHibernate(Session session)
    throws HibernateException, SQLException {
    int index = session.createSQLQuery(sql).executeUpdate();
    session.close();
    System.out.println(index);
    return (index == 1);
    }
    });
    }
      

  14.   

    delete from student_course_t  where student_id='1' and course_id='1'