资源描述:
《mysqlgroupby分组取值》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、groupby分组取值tb_FactoryNameIDfactoryJChengDateTime1a12009-5-52b12009-5-53c12009-5-54d12009-5-55a122009-5-46a222009-5-47a322009-5-48a422009-5-49b132009-4-410b232009-4-411b332009-4-312b432009-4-413c142009-3-314c242009-3-315c342009-3-3各位大侠我想达到这样的效果按照JCheng分组根据ID只取每组的前两个数据怎么实现啊?先谢谢了小弟分不多以
2、后有了再补上呵呵效果如下IDfactoryJChengDateTime1a12009-5-52b12009-5-53a122009-5-44a122009-5-45b132009-5-36b232009-5-3Select*fromtb_FactoryNameawhere(selectcount(1)fromtb_FactoryNamewhere[JCheng]=a.[JCheng]andID<=a.ID)<=2分组排序,取每组前五条记录--对表进行分组查询,在组内就进行排序,并且取得每组前5条记录.还要获得每组使用其它条件查询时候的总记录数--如:--字段K
3、ey为int型.select*fromTableNamewhereWord='FQ'andUserName='张三'groupbyKeyorderbyIDdescUNIONALLselect*fromTableNamewhereWord='FQ'andUserName='李四'groupbyKeyorderbyIDdescUNIONALLselect*fromTableNamewhereWord='FQ'andUserName='王五'groupbyKeyorderbyIDdesc--这里只是分组了,我还要获得每组(条件Word='FQ')的记录总数selec
4、tcount(ID)fromTableNamewhereWord='FQ'-----------------------------------------------------------上面的SQL是我的个人试想,实际运行是报错的.--最后返回记录集.组程序中DataSet----------------------------------------------------------网友回复:SQLcodeCodehighlightingproducedbyActiproCodeHighlighter(freeware)http://www.Code
5、Highlighter.com/--测试环境createtablestudent--学生列表(s_idint,s_namechar(10))createtableclass--课程列表(c_idint,c_namechar(10))createtablegrade--学生成绩表(s_idint,c_idint,cjint)--测试数据insertstudentvalues(1,'A')insertstudentvalues(2,'B')insertstudentvalues(3,'C')insertstudentvalues(4,'D')insertstude
6、ntvalues(5,'E')insertclassvalues(1,'历史')insertclassvalues(2,'数学')insertgradevalues(1,1,60)insertgradevalues(2,1,70)insertgradevalues(3,1,80)insertgradevalues(4,1,90)insertgradevalues(5,1,100)insertgradevalues(1,2,85)insertgradevalues(2,2,67)insertgradevalues(3,2,94)insertgradevalues
7、(4,2,63)insertgradevalues(5,2,87)selecta.c_id,a.s_id,a.cjfromgradeajoingradebona.c_id=b.c_idgroupbya.c_id,a.s_id,a.cjhavingcount(casewhena.cj<=b.cjthen1elsenullend)<=5--可动态修改orderbya.c_id,a.cjdesc--处理重复分数selecta.c_id,a.s_id,a.cjfromgradeajoin(selectc_id,cjfromgradegroupbyc_id,cj)bon
8、a.c_id=b.c_idgroupb