ifnull函数的使用
目的:从ta表中查询第二高xxx
select ifnull(
select distinct xxx
from ta
oreder by xxx desc
limit 1,1),null)
as 第二高xxx
步骤一:查询到表中第二高的值
select distinct xxx
from ta
order by xxx desc
limit 1,1
步骤二:特殊情况下,表中没有第二高的xxx,使用ifnull()函数进行查询
select if(步骤一,null)
补充:ifnull函数的语法
ifnull(a,b)函数解释:
如果value1不是空,结果返回a
如果value1是空,结果返回b
limit的语法
在《猴子 从零学会sql》中讲过:
limit n子句表示查询结果返回前n条数据
offset n表示跳过x条语句
limit y offset x 分句表示查询结果跳过 x 条数据,读取前 y 条数据
使用limit和offset,降序排列desc再返回第二条记录可以得到第二大的值