Home > Backend Development > PHP Tutorial > php 查询sql server 2008的问题

php 查询sql server 2008的问题

WBOY
Release: 2016-06-23 13:48:39
Original
1160 people have browsed it

我想要php分页从sql server 2008获取数据,

1.   mysql 可以 limit n,m  ,sql server没有
2.   要分页获取 union  后的数据, 网上看到有用rownum 和 top 的,请问怎么与union结合实现呢


我的结果集类似于

(select A,B,C,D from t1) union (select '100' as A ,B,C,D  from t2)  union (select A,B,C,D from t3)
Copy after login


想限制每次返回结果集的 哪一条到哪一条


回复讨论(解决方案)

这样写:

$sql = '你的sql查询指令';$start = 1; //起始偏移$pagesize = 20; //每页行数$sqls =<<< SQLSELECT * FROM (SELECT A.*, ROWNUM RN FROM ($sql) A WHERE ROWNUM < $start+$pagesize)WHERE RN >= $startSQL;
Copy after login
$sqls 就是最终执行的查询语句

理解了,原来我的问题在于 不知道给union 的结果取个别名,谢谢版主大人~~

最后结合百度 我是这样写的

SELECT * FROM (     SELECT  * , ROW_NUMBER() OVER (ORDER BY A DESC) AS rn          FROM  ($sql)  AS s1)AS s2  where rn between $from and $to
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template