我正在使用 Microsoft SQL Server 2012,我想运行这个看似简单的查询:
SELECT
FirstEvent.id AS firstEventID,
SecondEvent.id AS secondEventID,
DATEDIFF(second, FirstEvent.WndFGEnd, SecondEvent.WndFGStart) AS gap,
FirstEvent.TitleID = SecondEvent.TitleID AS titlesSameCheck
FROM VibeFGEvents AS FirstEvent
RIGHT OUTER JOIN VibeFGEvents AS SecondEvent
ON
FirstEvent.intervalMode = SecondEvent.intervalMode
AND FirstEvent.id = SecondEvent.id - 1
AND FirstEvent.logID = SecondEvent.logID
但是FirstEvent.TitleID = SecondEvent.TitleID AS titlesSameCheck
在SELECT
子句中是不正确的语法。但SELECT 子句 (Transact-SQL) 文档包含以下语法:
SELECT [ ALL | DISTINCT ]
[ TOP ( expression ) [ PERCENT ] [ WITH TIES ] ]
<select_list>
<select_list> ::=
{
*
| { table_name | view_name | table_alias }.*
| {
[ { table_name | view_name | table_alias }. ]
{ column_name | $IDENTITY | $ROWGUID }
| udt_column_name [ { . | :: } { { property_name | field_name }
| method_name ( argument [ ,...n] ) } ]
| expression
[ [ AS ] column_alias ]
}
| column_alias = expression
} [ ,...n ]
我认为这意味着表达式在 select 子句中是有效的,并且确实给出的示例包括1 + 2
. 查看表达式的文档:
{ constant | scalar_function | [ table_name. ] column | variable
| ( expression ) | ( scalar_subquery )
| { unary_operator } expression
| expression { binary_operator } expression
| ranking_windowed_function | aggregate_windowed_function
}
布尔相等检查是有效的表达式,实际上= (Equals) (Transact-SQL) 文档中给出的示例表达式包括一个:
SELECT DepartmentID, Name
FROM HumanResources.Department
WHERE GroupName = 'Manufacturing'
尽管在WHERE
条款中不是SELECT
条款。看起来我不能使用=
相等运算符来比较我的SELECT
子句中的表达式,因为它们被错误地解释为赋值。
如何FirstEvent.TitleID = SecondEvent.TitleID AS titlesSameCheck
在我的SELECT
子句中包含等效于的布尔相等列比较?