SELECT p_type,p_name,p_view,row_num from (
SELECT p_type,p_name,p_view,IF(@bak=p_type,@rownum:=@rownum+1,@rownum:=1) as row_num, @bak:=p_type
FROM
( SELECT p_type,p_name,p_view from products
order by p_type,p_view desc ) a , ( SELECT @rownum:=0,@bak:='') b
) c WHERE c.row_num <=2
IF函数
IF(条件,表达式1,表达式2)
如果条件成立 则返回表达式1,否则是2
转载于:https://blog.51cto.com/chinaleo/1934821