create table outlevelrecord(
id INT  PRIMARY KEY  AUTO_INCREMENT NOT NULL,
para VARCHAR,
value FLOAT,
crew VARCHAR,
recordtime DATETIME,
job VARCHAR
)
表结构如上,给定查询条件如下:
1  crew 
2  job
3  给定时间from,to,使得(from<=recordtime<=to)
4  给定一个int数据times,利用条件1,2,3以para分组,分组后将count(para)>=times的para找出来。

解决方案 »

  1.   

    表结构定义有点问题,更改如下:create table outlevelrecord(
    id INT  PRIMARY KEY  AUTO_INCREMENT NOT NULL,
    para VARCHAR(45),
    value FLOAT,
    crew VARCHAR(45),
    recordtime DATETIME,
    job VARCHAR(45)
    )
      

  2.   

    表结构有了,你的测试数据和正确结果是什么样? (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  3.   

    猜一下,看看下面语句是否猜中。
    select para
    from outlevelrecord
    where crew='aaaa'
    and job='jjjj'
    and recordtime between '2009-10-01' and '2009-10-31'
    group by para
    having count(*) > 10
      

  4.   

    不好意思!我的mysql版本是:5.1.41-community
    至于insert语句,我不大会,因为直接插入时间,我不会,那些时间格式我掌握的不好,后面还要请教这个问题的。因为我使用2010-01-15  21:55:30 这样的时间格式,结果插入做比较的时候,系统好像提示语法错误。当然,我这个是字符串格式的,直接进入查询语句的,因为我以前用2010-01-15形式的字符串,到数据库可以自动转化为时间类型,但是这种格式就出问题了。
      

  5.   


    select para,count(*) as times
    from outlevelrecord
    where crew='aaaa'
    and job='jjjj'
    and recordtime between '2009-10-01' and '2009-10-31'
    group by para
    having count(*) > 10
    我如果想把符合条件的记录的数目分别统计出来,这样改对不对?
      

  6.   

    自己试了,说是语法错误,
    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 ')' at line 1
      

  7.   


    //根据给定的起始时间,结束时间,工段(表名),班组,超限次数,查询符合条件的记录
      public ArrayList queryByCondition(String job,String crew,String from,String to,int times){
            Connection con=UtilClass.getCon();
            PreparedStatement pstat=null;
           // Statement stat=null;
            ResultSet rs=null;
           // ResultSetMetaData rsmd=null;
            ArrayList list=new ArrayList();
            OutTimes outTimes=null;
            try{
                 
                pstat=con.prepareStatement("select para,count(*) as times  from  outlevelrecord  where job=? and crew=? and recordtime between  ?  and  ?  group by para  having count(*)>?)");
                pstat.setString(1, job);
                pstat.setString(2, crew);
                pstat.setString(3, from);
                pstat.setString(4, to);
                pstat.setInt(5, times);            rs=pstat.executeQuery();            while(rs.next()) {
                    outTimes=new OutTimes();
                    outTimes.setCrew(crew);
                    outTimes.setParameterName(rs.getString("para"));
                    outTimes.setOutTimes(rs.getInt(times));
                    list.add(outTimes);
                }
                 /*
              //  pstat=con.prepareStatement("select * from  "+job);
                //pstat.setString(1, job);
               //rs=pstat.executeQuery();
                rsmd=rs.getMetaData();
                int number=rsmd.getColumnCount();
                for(int i=1;i<=number;i++){
                    String columnType=rsmd.getColumnTypeName(i);
                    String columnName=rsmd.getColumnName(i);
                    if(columnType.toLowerCase().equals("tinyint")){
                        outTimes=findOutTimes(job,crew,columnName,from,to,times);
                        if(outTimes.getOutTimes()>0)list.add(outTimes);
                    }
                }
                  * */        }catch(Exception e){
                e.printStackTrace();
            }
            finally{
                UtilClass.closeRs(rs);
               // UtilClass.closeStat(stat);
                UtilClass.closePstat(pstat);
                UtilClass.closeCon(con);
            }
             return  list;
        }