日期:2014-05-19  浏览次数:20682 次

Java中用sql 联合查询出现问题,求解。
数据库代码:
SQL code

select b.*,e.uid,e.ip,e.place,e.ctime,count(distinct e.uid) from edm.edm_open_56 e left join (select u.id,u.email from e_toemail u ) b on e.uid = b.id


运行数据库代码的结果:

id email uid ip place ctime count(distinct e.uid)
267 343193715@qq.com 267 192.168.4.200 - LAN 1340011706 1

Java代码:
Java code

public List<EmailOpenReturnInfoVO> findOpenInfo(Long taskId) {
        List<EmailOpenReturnInfoVO> emailOpenReturnInfoVOs = new ArrayList<EmailOpenReturnInfoVO>();
        String sql = "select b.*,e.uid,e.ip,e.place,e.ctime,count(distinct e.uid) from edm.edm_open_56 e left join (select u.id,u.email from e_toemail u ) b on e.uid = b.id";
        PreparedStatement ps = null;
        ResultSet rs = null;
        EmailOpenReturnInfoVO vo = null;
        Session session = null;
        try {
            session = this.sessionFactory.openSession();
            ps = SessionFactoryUtils.getDataSource(session.getSessionFactory()).getConnection().prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                vo = new EmailOpenReturnInfoVO();
                vo.setEmial(rs.getString("email"));
                vo.setIp(rs.getString("ip"));
                vo.setPlace(rs.getString("place"));
                vo.setTime(new Date(rs.getInt("ctime")*1000L));
                emailOpenReturnInfoVOs.add(vo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(null != session)
                session.close();
        }
        return emailOpenReturnInfoVOs;
    }


运行最后的结果:id = null ,email = null ;也就是e_toemail表中的所有数据都为null,但是第一张表中的数据能正常获取,求解啊。急急急

有能在线请教的吗?可以联系我的Q: 526673470

------解决方案--------------------
运行下sql看看数据
在vo打个断点、对比下
------解决方案--------------------
探讨

引用:

运行下sql看看数据
在vo打个断点、对比下

运行了SQL,sql是正确的,不是VO的问题,我设置了断点,获取:rs.getString("email") 的结果就是为NULL

------解决方案--------------------
vo.setEmial(rs.getString("email"));

把email转成大写试试
------解决方案--------------------
我很奇怪LZ的sql能执行?
count是聚合函数,如果select中除了count以外还有其它非聚合的字段存在,比如LZ的b.*,e.uid等,那么后面没有group by的话应该就是非法的