mysql中给查询结果添加序号

当我们想为查询结果添加序号的时候可以这样写

SELECT @rowid:=@rowid+1 allid, pan_number, receive_person,time_note, url,unit_name,id
FROM(
SELECT a.pan_number,d.F_RealName receive_person, a.time_note,b.url, c.unit_name, c.id
FROM medium_receiptlist_pan a
left join medium_receiptlist_voucher b on a.pan_number = b.pan_number
left join r_counterpart_unit c on a.company = c.id
left join hswj_user d on a.receive_person = d.F_Id
WHERE 
 pan_logo = '%s'
ORDER BY a.add_time DESC) a, (SELECT @rowid:=0) b

 

 

 思想:事先定义一个变量,通过变量的递加以及虚拟表的联查达到生成序列号的目的。

posted @ 2020-01-06 11:08  YF-海纳百川  阅读(791)  评论(0编辑  收藏  举报