Mysql 如何查询最小值?
表 PTest 结构结构如下
PCode PCount
7 3
8 2
9 3
10 4
11 2
查询结果是
PCode PCount
8 2
规则是:先考虑PCount最小,再考虑PCode最小
(注:PCode和PCount都是无序的,Pcode 唯一)
------解决方案--------------------SELECT * from PTest WHERE PCode=(SELECT min(PCode) FROM PTest WHERE PCount=(SELECT min(PCount) from PTest))
经验证可以。
------解决方案--------------------select pcode,pcount from ptest where pcount=(select min(pcount) from ptest) order by pcode limit 1
估计应该可以,没测试过。
------解决方案--------------------SQL code
SELECT * from PTest WHERE PCode=(SELECT min(PCode) FROM PTest WHERE PCount=(SELECT min(PCount) from PTest)) and PCount=(select min(PCount) from PTest)
------解决方案--------------------
select *
from ptest
order by pcount,pcode limit 1