使用hibernate的this.getSession().createSQLQuery(sql).list();方法查询数据时出现查到的数据和想象的不一致,很是郁闷,诡异

今天 使用hibernate的this.getSession().createSQLQuery(sql).list();方法查询数据时出现查到的数据和想象的不一致的问题,郁闷我很长一段时间

执行的方法代码如下:

public List<?> findKaoHe4Data(String startDay, String endDay, String depIds) {
        String sql = "SELECT t.dep_id,t.memo,"
                + "sum(case ISNUMERIC(t.chanl) when 1 then cast(t.chanl as float) else 0 end) as chanl,"
                + "sum(case ISNUMERIC(t.cail) when 1 then cast(t.cail as float) else 0 end) as cail,"
                + "sum(case ISNUMERIC(t.dianf) when 1 then cast(t.dianf as float) else 0 end) as dianf "
                + "FROM team_settle t  JOIN department d on d.dep_id=t.dep_id  JOIN department pd on d.pdep_id = pd.dep_id WHERE d.pdep_id IN ("
                + depIds + ") and settle_date >='" + startDay + "' and settle_date<='" + endDay + "' and t.memo<>'' "
                + "GROUP BY t.dep_id,t.memo,pd.show_order,d.show_order ORDER BY pd.show_order,d.show_order";
        return this.getSession().createSQLQuery(sql).list();
    }


截取到组合好的sql语句如下:

View Code
SELECT
        t.dep_id,
        t.memo,
        sum(case ISNUMERIC(t.chanl) 
            when 1 then cast(t.chanl as float) 
            else 0 
        end) as chanl,
        sum(case ISNUMERIC(t.cail) 
            when 1 then cast(t.cail as float) 
            else 0 
        end) as cail,
        sum(case ISNUMERIC(t.dianf) 
            when 1 then cast(t.dianf as float) 
            else 0 
        end) as dianf 
    FROM
        team_settle t  
    JOIN
        department d 
            on d.dep_id=t.dep_id  
    JOIN
        department pd 
            on d.pdep_id = pd.dep_id 
    WHERE
        d.pdep_id IN (
            '402882eb369b1a0601369b288b8e0006','402882eb369b1a0601369b28d3360007','402882eb369b1a0601369b293e820008','402882eb369b1a0601369b29efe10009','402882eb369b1a0601369b2a25b6000a','402882eb369b1a0601369b2a5e1b000b'
        ) 
        and settle_date >='2013-01-01' 
        and settle_date<='2013-01-16' 
        and t.memo<>'' 
    GROUP BY
        t.dep_id,
        t.memo,
        pd.show_order,
        d.show_order 
    ORDER BY
        pd.show_order,
        d.show_order

拿到navcat中执行以下代码,得到的是15条记录

但是java类中 的list集合却显示的是9条记录

为什么两处的查询结果不一致呢,找不到原因,以致使用hibernate  show_sql功能,在控制台打印的sql,

拿到navcat中执行以下仍然是15条记录,疯了,疯了,崩溃了,啥子原因...

疯了之后,把meyeclipse关闭了重启,还是不好使,疯了,疯了。。。

就在忽然间,看到了在navcat中执行查询的数据库是服务器的数据库,然而在项目中连接的数据库是本地的数据库,

忽然间我仰天大笑,仰天大笑,原来是这原因,疯了疯了

 

 

 

posted @ 2013-01-19 23:07  wanggd_blog  阅读(13201)  评论(3编辑  收藏  举报