简体   繁体   English

行PHP PDO中的MSSQL查询

[英]MSSQL Query in Groups of Rows PHP PDO

I've been trying to get this query working for far too long now, and I figured it was time to ask for help. 我一直在努力使此查询工作太久了,我认为现在是时候寻求帮助了。 I'm currently trying to query a MSSQL database from a PHP script using the PDO connection type. 我目前正在尝试使用PDO连接类型从PHP脚本查询MSSQL数据库。 I'm trying to get all contents from the rows between certain values (here it's 100 and 200). 我正在尝试从某些值(这里是100和200)之间的行中获取所有内容。 This is the query I'm trying to run. 这是我要运行的查询。

SELECT TOP 100 Id, CreateDate, cast(XmlBody as varchar(max))
as XmlBody, ObjectType, Tag, Name
FROM Table WHERE ObjectType LIKE 'StaffPersonal' AND BETWEEN 100
AND 200

I typically use MYSQL, so I'm a little lost on the proper syntax for MSSQL (the LIKE keyword was a brutal lesson). 我通常使用MYSQL,所以我对MSSQL的正确语法有些迷茫(LIKE关键字是一个残酷的教训)。 This query is throwing a general syntax error. 该查询引发一般语法错误。

exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 156 General SQL Server error: Check messages from the SQL Server [156] (severity 15) 消息“ SQLSTATE [HY000]”的异常“ PDOException”:常规错误:156常规SQL Server错误:检查来自SQL Server的消息[156](严重性15)

I'm not sure what's causing this error, but it's driving me crazy. 我不确定是什么原因导致了此错误,但这使我发疯。 Thanks in advance for the help, it is truly appreciated. 在此先感谢您的帮助,我们非常感谢。

If I understand it correctly and you are trying to get the records 100 to 200 from the bigger recordset. 如果我理解正确,并且您正尝试从较大的记录集中获取记录100至200。 You can use the below statement. 您可以使用以下语句。

SELECT * FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY Id ) AS RowNum, Id, CreateDate, cast(XmlBody as varchar(max)) as XmlBody, ObjectType, Tag, Name FROM Table WHERE ObjectType = 'StaffPersonal' ) AS RowConstrainedResult WHERE RowNum >= 100 AND RowNum < 201 ORDER BY RowNum

Above query uses a subquery and assigns Row numbers based on the Id field (ordered by Id) then filters the result set for the Row numbers 100 to 200. 上面的查询使用一个子查询,并根据Id字段分配行号(按ID排序),然后过滤行号100到200的结果集。

You can also use the below where statement if you want to use BETWEEN operator 如果要使用BETWEEN运算符,也可以使用以下where语句

WHERE RowNum BETWEEN 100 AND 200 

Between requires a field before it. 之间需要一个字段。 In this case you need the ROW_NUMBER() function to give you that. 在这种情况下,您需要ROW_NUMBER()函数来实现。 I also changed the LIKE to = to make it more efficient since you were looking for exact match. 我也将LIKE更改为=,以提高效率,因为您正在寻找完全匹配的内容。

To try the simplified version on SQLFiddle SQLFiddle上尝试简化版本

Table and data creation 表和数据创建

CREATE TABLE supportContacts 
(
 id int  primary key, 
 type varchar(20), 
 details varchar(30)
);

INSERT INTO supportContacts
(id, type, details)
VALUES
(1,'Email', 'admin@sqlfiddle.com'),
(2,'Twitter', '@sqlfiddle'),
(3,'Twitter2', '@sqlfiddle'),
(4,'Twitter3', '@sqlfiddle');

And select query 并选择查询

SELECT  *
FROM    ( SELECT    ROW_NUMBER() OVER ( ORDER BY Id ) AS RowNum, Id, type, details
      FROM      supportContacts
      WHERE     type = 'twitter'
    ) AS RowConstrainedResult
--WHERE   RowNum >= 1
--    AND RowNum < 2
WHERE RowNum BETWEEN 1 AND 2 
ORDER BY RowNum

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM