标签:sele 分类 rank rom ble 临时 计数 desc where
select a.name Aname,b.*,c.* from navtype_table a,
(
SELECT NumSerial,navtype_id
FROM
(SELECT NumSerial,navtype_id,
@navtype_id_rank := IF(@current_navtype_id = navtype_id, @navtype_id_rank + 1, 1) AS navtype_id_rank,
@current_navtype_id := navtype_id
FROM navtype_one_table
ORDER BY navtype_id DESC
) rank
WHERE rank.navtype_id_rank <= 2
) b,
mainbooks c
where a.ID = b.navtype_id and b.NumSerial = c.NumSerial
ORDER BY c.NumSerial,navtype_id
以上a表为主类表,b为中间表,c为副类表。
实现原理副表的外键做排序计数并添加为临时字段,出现一次为1,第二次为2,依次类推。
之后在where条件下对临时字段进行限制即可求出每个分类下的前n条数据。
标签:sele 分类 rank rom ble 临时 计数 desc where
原文地址:https://www.cnblogs.com/bzdofj/p/13806830.html