关于SQL语句:MySql转成SQLServer
Stringsql="select*fromec_order"+clause+"orderbyiddesclimit"+(page-1)*pagesize+","+pag...
String sql = "select * from ec_order " + clause
+ " order by id desc limit " + (page - 1) * pagesize + ","
+ pagesize;
上面这句是mysql的sql语句,但是我不会用mysql只会sqlserver,请问上面这句怎么转成sqlserver的语句?请大神帮忙啊! 展开
+ " order by id desc limit " + (page - 1) * pagesize + ","
+ pagesize;
上面这句是mysql的sql语句,但是我不会用mysql只会sqlserver,请问上面这句怎么转成sqlserver的语句?请大神帮忙啊! 展开
推荐于2016-04-09
展开全部
假设 clause = where 1=1
假设 pagesize = 10 page = 1
也就是每页10行数据, 查询第1页
-- MYSQL 下面执行的语句:
select * from ec_order
where 1=1
order by id desc limit 0 , 10;
-- 切换到 SQL Server
select top 10 *
from ec_order
where 1=1
order by id desc
假设 pagesize = 10 page = 2
也就是每页10行数据, 查询第2页
-- MYSQL 下面执行的语句:
select * from ec_order
where 1=1
order by id desc limit 10 , 10;
-- 切换到 SQL Server 方案1.
select top 10 *
from ec_order
where 1=1 AND id NOT IN (SELECT top 10 id FROM ec_order where 1=1 order by id desc)
order by id desc
-- 切换到 SQL Server 方案2.
SELECT *
FROM
(
select ROW_NUMBER() OVER(ORDER BY id desc) AS NO, ec_order.*
from ec_order
where 1=1
)
WHERE NO between 11 and 20
-- 切换到 SQL Server 方案3.
select top 20 * from ec_order where 1=1 order by id desc
EXCEPT
select top 10 * from ec_order where 1=1 order by id desc
展开全部
--第一种方式[针对数据量较小的分页统计]
drop table #tmp;
Select identity(int,1,1) as id,* into #tmp from songlist
select sourcefilename ,songname,videotype,singerid,singer,songpic from #tmp where id between 200 and 300;
select count(*) as n1,ceiling(count(*)/200) as n2 from songlist;
--第二种方式[主要针对数据量稍微大点的 分页统计 先按照主键排序]
declare @pagerow int ,@pageid int
set @pagerow = 100;--每页显示的个数
set @pageid = 2;--第几页
select top @pagerow sourcefilename, songname,videotype,singerid,singer,songpic from songlist
where sourcefilename not in (select top @pagerow*(@pageid-1) sourcefilename from songlist order by sourcefilename)
order by sourcefilename;
drop table #tmp;
Select identity(int,1,1) as id,* into #tmp from songlist
select sourcefilename ,songname,videotype,singerid,singer,songpic from #tmp where id between 200 and 300;
select count(*) as n1,ceiling(count(*)/200) as n2 from songlist;
--第二种方式[主要针对数据量稍微大点的 分页统计 先按照主键排序]
declare @pagerow int ,@pageid int
set @pagerow = 100;--每页显示的个数
set @pageid = 2;--第几页
select top @pagerow sourcefilename, songname,videotype,singerid,singer,songpic from songlist
where sourcefilename not in (select top @pagerow*(@pageid-1) sourcefilename from songlist order by sourcefilename)
order by sourcefilename;
追问
能举个例子吗?看不懂上面的代码啊?
追答
就是给ec_order 表添加一个字段 让他自动增长 然后插入到临时表中 然后直接用between and 查询 要查的 区间就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询