使用MySQL查询每月考勤报告

时间:2015-02-02 10:14:41

标签: java mysql

我有这样的用户表

user_id | user_name
--------+----------
      1 | peter
      2 | robert
      3 | stive

像这样的出勤表

id | user_id | status |   date
---+---------+--------+-----------
 1 |       1 |     p  | 12-16-2014
 2 |       2 |     p  | 12-16-2014
 3 |       3 |     a  | 12-16-2014
 4 |       1 |     p  | 12-17-2014
 5 |       2 |     a  | 12-17-2014
 6 |       3 |     a  | 12-17-2014
 7 |       1 |     p  | 12-18-2014
 8 |       2 |     a  | 12-18-2014
 9 |       3 |     p  | 12-18-2014

但我希望显示如下结果

user_id | user_name   | 12-16-2014 | 12-17-2014 | 12-18-2014
--------+-------------+------------+------------+-----------
      1 | peter       |          p |          p |         p
      2 | robert      |          p |          a |         a
      3 | stive       |          a |          a |         p

选择应基于月份或周,具体取决于用户提供的日期。

我尝试在这样的java中执行查询,但它无法正常工作

String setsqlMax = "SET SESSION group_concat_max_len = 100000000000;";
        String setsql ="SET @sql = NULL;";
        String dynamicsql = 

                       "SELECT"+
                       " GROUP_CONCAT(DISTINCT"+
                       " CONCAT("+
                       "'max(CASE WHEN attendance.attendance_date = ''',"+
                       " date_format(date, '%Y-%m-%d'),"+
                       " ''' THEN coalesce(p.present, '''') END) AS `',"+
                       " date_format(date, '%Y-%m-%d'), '`'"+
                       ")"+
                       " ) INTO @sql"+
                       " FROM calendar"+
                       " where date>='01-01-2015'"+
                       "  and date <= '31-01-2015';";


        String mainsql   =  "SET @sql = CONCAT('SELECT p.user_id,',@sql,'"+
                        " from"+
                        "("+
                        "select c.user_id"+
                        " from calendar c"+
                        " cross join attendance a"+
                        " )"+
                        " attendance LEFT JOIN attendance p"+
                        " on attendance.user_id= p.user_id"+
                       " and attendance.date = p.attendance_date"+
                       " where attendance.date>=''01-01-2015''"+
                       "  and attendance.date <= ''31-01-2015''"+
                       " group by attendance.user_id') ;";
           String preparesql = " PREPARE stmt FROM @sql";
           String ExcuteSql = "EXECUTE stmt ;";
           String deallocateSql = "DEALLOCATE PREPARE stmt;";
 String totalQuery = setsqlMax + setsql + dynamicsql + mainsql + preparesql+ ExcuteSql + deallocateSql; 

这在MySQL查询浏览器中完全执行而不是java 错误是

com.mysql.jdbc.exceptions.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 'SET @sql = NULL;SELECT GROUP_CONCAT(DISTINCT CONCAT('max(CASE WHEN attendance.at' at line 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3249)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1403)
    at com.nippon.dao.AttendanceDao.viewAttendanceMonthWise(AttendanceDao.java:408)
    at com.nippon.service.AttendanceService.viewAttendanceMonthlyReport(AttendanceService.java:316)
    at com.nippon.facade.AttendanceFacade.viewAttendanceMonthlyReport(AttendanceFacade.java:37)
    at com.nippon.web.servants.AttendanceController.processRequest(AttendanceController.java:144)
    at com.nippon.web.frontcontroller.FrontController.doGet(FrontController.java:150)
    at com.nippon.web.frontcontroller.FrontController.doPost(FrontController.java:169)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)

0 个答案:

没有答案