利用limit取靠近值

xiaoxiao2026-06-11  10

String tz=patientinfo.getBrtz(); String age=patientinfo.getNl(); String sql="select Fhz from who_age_weight where Sex='"+sex+"' and Weight='"+tz+"' and Age='"+age+"'"; int i=0; String k5=""; i=igeneralDAO.count(sql); if(i==0){//没有符合条件的值,那只能利用靠拢原则进行判断,取离其值最近的作为标准 /***********************************取接近tz的最大值***************************/ String sql1="select Weight from who_age_weight where Age='"+age+"' and Sex='"+sex+"' and Weight>="+tz+ " limit 1"; String sql2="select Weight from who_age_weight where Age='"+age+"' and Sex='"+sex+"' and Weight<="+tz+" group by Weight desc limit 1"; /*********************************** 取接近tz的最小值***************************/ List list1=igeneralDAO.findBySQL(sql1); Iterator it1=list1.iterator(); double tz1=Double.parseDouble(tz); double k2=0.0; double k1=0.0; System.out.println("你输入的的体重是"+tz); if(it1.hasNext()==false){ k5="没有符合这个体重的测试值,请检查信息是否输入错误"; return k5; } while(it1.hasNext()){ k1=Double.parseDouble(it1.next().toString());//最大值 System.out.println("最大值k1:"+k1); } List list2=igeneralDAO.findBySQL(sql2);//最小值 Iterator it2=list2.iterator(); if(it2.hasNext()==false){ k5="没有符合这个体重的测试值,请检查信息是否输入错误"; return k5; } while(it2.hasNext()){ k2=Double.parseDouble(it2.next().toString()); System.out.println("最小值k2:"+k2); } System.out.println("(tz1-k2):"+(tz1-k2)); System.out.println("(k1-tz1):"+(k1-tz1)); if((tz1-k2)>=(k1-tz1)){ String k3=String.valueOf(k1); String sql3="select Fhz from who_age_weight where Sex='"+sex+"' and Weight='"+k3+"' and Age='"+age+"'"; System.out.println("离最大值"+k3+"差距更小"+sql3); List list=igeneralDAO.findBySQL(sql3); Iterator it=list.iterator(); while(it.hasNext()){ k5=it.next().toString(); } return k5; }else { String k6=String.valueOf(k2); String sql4="select Fhz from who_age_weight where Sex='"+sex+"' and Weight='"+k6+"' and Age='"+age+"'"; System.out.println("离最小值"+k6+"差距更小"+sql4); List list=igeneralDAO.findBySQL(sql4); Iterator it=list.iterator(); while(it.hasNext()){ k5=it.next().toString(); } return k5; } }else if(i>1){ //利用查询后有符合条件的数据直接返回所查到的值 List list=igeneralDAO.findBySQL(sql); Iterator it=list.iterator(); while(it.hasNext()){ k5=it.next().toString(); } return k5; } return null; }

 

转载请注明原文地址: https://www.6miu.com/read-5049984.html

最新回复(0)