资源描述:
《查找某个字段最大值的记录 sql语句》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、查找某个字段最大值的记录SQL语句 selecttable_name.*fromtable_name,(selectmax(price)asprice,pidfromtable_namegroupbypid)astable_name_tempwheretable_name_temp.price=table_name.priceandtable_name_temp.pid=table_name.pid;--SQLcodecreatetablelk1(uidint,pidint,priceint,`time`date)engine=myisam;inser
2、tintolk1values(1,1,100,'2007-07-01'),(1,2,150,'2007-07-02'),(2,1,110,'2007-07-03'),(3,1,120,'2007-07-04'),(4,2,180,'2007-07-04'),(3,2,170,'2007-07-04'),(6,3,130,'2007-07-04');select*fromlk1wherepricein(selectmax(price)fromlk1groupbypid)groupbypid;--结果1:queryresult(3records)uidpid
3、pricetime311202007-07-04421802007-07-04631302007-07-04truncatetablelk1;insertintolk1values(1,1,200,'2007-07-01'),(1,2,200,'2007-07-02'),(2,1,110,'2007-07-03'),(3,1,120,'2007-07-04'),(4,2,180,'2007-07-04'),(3,2,170,'2007-07-04'),(6,3,130,'2007-07-04');select*fromlk1wherepricein(se
4、lectmax(price)fromlk1groupbypid)groupbypid;--结果2:queryresult(3records)uidpidpricetime112002007-07-01122002007-07-02631302007-07-04根据mysql的手册上面找到一个查询,修改了一下发现也可以实现,但是并不理解什么意思,不知道有没有人给解释一下SQLcodeSELECT*FROMlk1l1WHEREprice=(SELECTMAX(l2.price)FROMlk1l2WHEREl1.`pid`=l2.`pid`);--SQLcod
5、ecreatetablelk1(uidint,pidint,priceint,`time`date)engine=myisam;insertintolk1values(1,1,100,'2007-07-01'),(1,2,150,'2007-07-02'),(2,1,110,'2007-07-03'),(3,1,120,'2007-07-04'),(4,2,180,'2007-07-04'),(3,2,170,'2007-07-04'),(6,3,130,'2007-07-04');select*from(select*fromlk1orderbypri
6、cedesc)Tgroupbypid;--结果1:queryresult(3records)uidpidpricetime311202007-07-04421802007-07-04631302007-07-04truncatetablelk1;insertintolk1values(1,1,200,'2007-07-01'),(1,2,200,'2007-07-02'),(2,1,110,'2007-07-03'),(3,1,120,'2007-07-04'),(4,2,180,'2007-07-04'),(3,2,170,'2007-07-04'),
7、(6,3,130,'2007-07-04');select*from(select*fromlk1orderbypricedesc)Tgroupbypid;--结果2:queryresult(3records)uidpidpricetime112002007-07-01122002007-07-02631302007-07-04insertintolk1values(4,3,200,'2007-07-05'),(5,3,210,'2007-07-05');select*from(select*fromlk1orderbypricedesc)Tgroupb
8、ypid;--结果3:queryresult(3records)uidpidpr