我为SQL Server编写了一个SQL查询,它在SQL Server Management Studio中可以完美地工作。但是,当我在PHP代码中运行相同的查询时,它将返回错误“语法错误'='”。我已经运行了不同的查询,然后才能正常运行。
$SQL = "SELECT MAX(EventTime) AS EventTime, UserID
FROM [Net2Events].[dbo].[Events] e
WHERE EventType = 20 AND
Address = 5044830 AND
NOT EXISTS (Select Max(EventTime) AS EventTime
FROM [Net2Events].[dbo].[Events] e2
WHERE Address = 5042486 AND
e2.EventTime > e.EventTime AND
e2.UserId = e.UserId AND
EventType = 20
GROUP BY UserId)
GROUP BY UserID";
答案 0 :(得分:1)
尝试像这样运行查询:
$result = mssql_query('SELECT MAX(EventTime) AS EventTime, UserID
FROM [Net2Events].[dbo].[Events] e
WHERE EventType = 20 AND
Address = 5044830 AND
NOT EXISTS (Select Max(EventTime) AS EventTime
FROM [Net2Events].[dbo].[Events] e2
WHERE Address = 5042486 AND
e2.EventTime > e.EventTime AND
e2.UserId = e.UserId AND
EventType = 20
GROUP BY UserId)
GROUP BY UserID');
答案 1 :(得分:0)
尝试将所有内容全部放在一行中。我知道它看起来很可怕,但是它将帮助您了解是否需要以不同的方式来满足多行字符串的需要。
$SQL = "SELECT MAX(EventTime) AS EventTime, UserID FROM [Net2Events].[dbo].[Events] e WHERE EventType = 20 AND Address = 5044830 AND NOT EXISTS (Select Max(EventTime) AS EventTime FROM [Net2Events].[dbo].[Events] e2 WHERE Address = 5042486 AND e2.EventTime > e.EventTime AND e2.UserId = e.UserId AND EventType = 20 GROUP BY UserId) GROUP BY UserID";
如果有效,那么您可以缩小问题范围。您可以尝试在每行的末尾添加一个空格。
答案 2 :(得分:0)
请尝试在方括号中包装“地址”列的所有实例。这是一个SQL Server关键字,可能会混淆驱动程序。
$SQL = "SELECT MAX(EventTime) AS EventTime, UserID
FROM [Net2Events].[dbo].[Events] e
WHERE EventType = 20 AND
[Address] = 5044830 AND
NOT EXISTS (Select Max(EventTime) AS EventTime
FROM [Net2Events].[dbo].[Events] e2
WHERE [Address] = 5042486 AND
e2.EventTime > e.EventTime AND
e2.UserId = e.UserId AND
EventType = 20
GROUP BY UserId)
GROUP BY UserID";