set nocount on --SQL Server 的话,放在一条语句中加上此句
select mid=0,* into #t from 产品表 order by mt_id
declare @i int,@mt_id int
update #t set @i=case @mt_id when mt_id then @i+1 else 1 end
,mid=@i,@mt_id=mt_id
select *
from 用户表 a join #t b on a.mt_id=b.mt_id
where a.usertype=1 or (a.usertype=0 and b.mid<=5)
drop table #t
select mid=0,* into #t from 产品表 order by mt_id
declare @i int,@mt_id int
update #t set @i=case @mt_id when mt_id then @i+1 else 1 end
,mid=@i,@mt_id=mt_id
select *
from 用户表 a join #t b on a.mt_id=b.mt_id
where a.usertype=1 or (a.usertype=0 and b.mid<=5)