NutzCN Logo
问答 sql里查看当前日期是否比指定日期大
发布于 3180天前 作者 qq_c6efc20e 2403 次浏览 复制 上一个帖子 下一个帖子
标签:

这个sql放在数据库里执行没有问题。为哈程序一执行就报错了。
sql:
sql = Sqls.queryRecord("select * from activity apt where is_remove=0 and type=1 and CURDATE()<apt.endDate ORDER BY id desc");
异常:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'CURDATE()<endDate' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

7 回复

报错信息跟sql不太一样吧?

CURDATE()<apt.endDate use near 'CURDATE()<endDate' at line 1

@wendal 嗯,apt.是我发帖的时候加上去的,标明下是表的属性哈哈,不知道这是什么情况

那你还得在贴一次真正的代码

@wendal

             if (curPage == 0) {
            curPage = 1;
        }
        try {
            Pager pager = dao.createPager(curPage, 10);//当前页,每页显示的个数,
            sqlCount = Sqls.fetchInt("select count(*) from activity  where  is_remove=0 and type=1and  CURDATE()<apt.endDate");//设置sql查询条件
            pager.setRecordCount(dao.execute(sqlCount).getInt());//得到查询出来的总记录数,放入page对象

            sql = Sqls.queryRecord("select * from activity apt  where  is_remove=0 and type=1 and CURDATE()<apt.endDate ORDER BY id desc");
            sql.setPager(pager);
            dao.execute(sql);
            List<Record> list = sql.getList(Record.class);
            map.put("recordList",list);
            map.put("pager",pager);
            map.put("result",1);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",2);
        }

下面是异常信息:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'CURDATE()<apt.endDate' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
at com.mysql.jdbc.Util.getInstance(Util.java:382)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2620)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2570)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1474)
at org.apache.commons.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:208)
at org.apache.commons.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:208)
at org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:200)
at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:43)
... 43 more

@wendal 我找到原因了 and type=1and CURDATE()<apt.endDate。。1和and中没有空格

@qq_c6efc20e 一个空格引发的血案

添加回复
请先登陆
回到顶部