zoukankan      html  css  js  c++  java
  • sql总结:



       1:in查询(以逗号隔开的字符串)
        例如:查找数据库中用户E-mail,条件是必须是以下用逗号格开的用户E-mail,如果用以下SQL语句执行,则结果是正确的:
        sql="select email from uereTable where email in ('小王','小赵','小张','小候','小董')"
        但是,如果将这些关键字匹配字连接成字符串再查询,就得不到正确的结果了:
        String personNames = "小王,小赵,小张,小侯,小董";
        sql="select email from uereTable where email in (personNames)"
        这是因为此时的sql文相当于:sql="select email from uereTable where email in ("小王,小赵,小张,小侯,小董")",该句会去搜索一个名叫“小王,小赵,小张,小侯,小董”的人,所以当然搜不到啦。解决办法如下:
            <script language=vbscript>
    keyword="小王,小赵,小张,小候,小董"
    keyword = Replace(keyword,",","','")
    sql=" select email from uereTable where email in ('" + keyword + "') "
    alert(sql)
    </script>
        如果是在后台处理,在JAVA中则可以这样来处理:
        String personNames = "小王,小赵,小张,小侯,小董";
        personNames = personNames.replaceAll(",", "','");
        sql="select email from uereTable where email in ('" + personNames + "')"

        如果要求in后面的多个查询条件关键字都是模糊匹配,似乎目前找不到这样的解决办法,只能通过循环,使用like模糊查询语句,如下:
                keyword="王,赵,张,候,董";
                sql=" select email from uereTable ";      

                String[] keywordArray = sectLinkName.split(",");
                for(int i = 0; i < keywordArray .length; i++){
                    
                    if(i == 0){
                        sql.append("where emial like '%" + keywordArray [i] + "'% ");
                    } else {
                        sql.append("or"+ "emial  like '%" + keywordArray [i] + "'% ");
                    }
                }

        2:用Hibernate来执行sql语句:
       
        Java代码 复制代码
        SQLQuery query = null;   
        query = session.createSQLQuery("select * from fun as ca,(select * from comment as r1 where r1.created_at>=all(select r2.created_at from comment as r2 where r1.fun_id=r2.fun_id)) as re where ca.id=re.fun_id order by re.created_at desc;");   
        List<Fun> list = query.addEntity(Fun.class).list();  
        SQLQuery query = null;
        query = session.createSQLQuery("select * from fun as ca,(select * from comment as r1 where r1.created_at>=all(select r2.created_at from comment as r2         where r1.fun_id=r2.fun_id)) as re where ca.id=re.fun_id order by re.created_at desc;");
        List<Fun> list = query.addEntity(Fun.class).list();

        如果没有最后一行的addEntity方法,在执行过程中会产生

        Java代码 复制代码
        org.hibernate.MappingException: No Dialect mapping for JDBC type: -1  
        org.hibernate.MappingException: No Dialect mapping for JDBC type: -1
     类似的异常!
  • 相关阅读:
    [JSOI2008]巨额奖金(最小生成树计数)
    [HAOI2008] 糖果传递
    [SCOI2009]生日快乐
    BZOJ2821 作诗
    [HAOI2008]圆上的整点
    POJ1741
    AC自动机
    [JSOI2008]星球大战starwar
    二分图有关证明(感性版)
    初识Pentaho(一)
  • 原文地址:https://www.cnblogs.com/pricks/p/1601856.html
Copyright © 2011-2022 走看看