我正在使用 Zend Framework 1.12 访问 MSSQL 2008 服务器。我使用 FreeTDS 作为数据库驱动程序。
我正在使用 Zend_Db 生成以下查询。
$obj_sel = $obj_db
-> select ()
-> from (array ('ps' => 'ProductStock'), array ('PLU', 'stock' => 'SUM(ps.stock)'))
-> join (array ('pc' => 'ProductCatalogue'), 'ps.PLU = pc.PLU', NULL)
-> join (array ('gl' => 'Gemini_Location'), 'ps.Location = gl.LocationID', array ('LocationID'))
-> where ('ps.status = 1')
-> where ('ps.PLU IS NOT NULL');
> where ('pc.rootPLU >= ?', $this -> int_start_rootplu);
-> group ('ps.PLU')
-> group ('gl.LocationID')
-> order (array ('ps.PLU', 'gl.LocationID'));
如果我运行并执行它,我会得到一个似乎工作正常且正确的查询。
SELECT "ps"."PLU", SUM(ps.stock) AS "stock", "gl"."LocationID" FROM "ProductStock" AS "ps"
INNER JOIN "ProductCatalogue" AS "pc" ON ps.PLU = pc.PLU
INNER JOIN "Gemini_Location" AS "gl" ON ps.Location = gl.LocationID WHERE (ps.status = 1) AND (ps.PLU IS NOT NULL) AND (pc.rootPLU >= 93838) GROUP BY "ps"."PLU",
"gl"."LocationID" ORDER BY "ps"."PLU" ASC, "gl"."LocationID" ASC
但是当我尝试像这样向查询添加限制或偏移量时:
$obj_sel = $obj_db
-> select ()
-> from (array ('ps' => 'ProductStock'), array ('PLU', 'stock' => 'SUM(ps.stock)'))
-> join (array ('pc' => 'ProductCatalogue'), 'ps.PLU = pc.PLU', NULL)
-> join (array ('gl' => 'Gemini_Location'), 'ps.Location = gl.LocationID', array ('LocationID'))
-> where ('ps.status = 1')
-> where ('ps.PLU IS NOT NULL');
> where ('pc.rootPLU >= ?', $this -> int_start_rootplu);
-> group ('ps.PLU')
-> group ('gl.LocationID')
-> order (array ('ps.PLU', 'gl.LocationID'))
-> limit (1000,2000);
我得到以下 SQL 服务器拒绝执行的查询。
SELECT * FROM (SELECT TOP 1000 * FROM (SELECT TOP 3000 "ps"."PLU", SUM(ps.stock) AS "stock", "gl"."LocationID" FROM "ProductStock" AS "ps"
INNER JOIN "ProductCatalogue" AS "pc" ON ps.PLU = pc.PLU
INNER JOIN "Gemini_Location" AS "gl" ON ps.Location = gl.LocationID WHERE (ps.status = 1) AND (ps.PLU IS NOT NULL) AND (pc.rootPLU >= 93838) GROUP BY "ps"."PLU",
"gl"."LocationID" ORDER BY "ps"."PLU" ASC, "gl"."LocationID" ASC) AS inner_tbl ORDER BY "ps"."PLU" , "gl"."LocationID" DESC) AS outer_tbl ORDER BY "ps"."PLU" , "gl"."LocationID" asc
我收到以下错误:
SQLSTATE [HY000]:一般错误:4104 一般 SQL Server 错误:检查来自 SQL Server [4104](严重性 16)[(null)] 的消息
我对 MSSQL 不像对 MySQL 或 Postgres 那样熟悉,所以我将不得不假设 TOP 技术是正确的。但是,很明显,这里生成的任何 SQL Zend DB 都是不正确的。
这是 Zend DB 中的一个已知问题吗?如果是这样,我该如何解决它?