如何将此 MySQL 查询转换为 SQL Server 等效项?
SELECT User.id, User.first, User.last,
IF (User.first = q1 OR User.first = q2, 1, 0) +
IF (User.last = q1 OR User.last = q2, 1, 0) as points
FROM users as User
ORDER BY points DESC
如何将此 MySQL 查询转换为 SQL Server 等效项?
SELECT User.id, User.first, User.last,
IF (User.first = q1 OR User.first = q2, 1, 0) +
IF (User.last = q1 OR User.last = q2, 1, 0) as points
FROM users as User
ORDER BY points DESC
IF
SQL Server 中的等价物是CASE
表达式。(或IIF
在 SQL Server 2012中。)我还删除了在这种情况下似乎毫无意义的别名。
SELECT id, first, last,
CASE WHEN first IN (q1,q2) THEN 1 ELSE 0 END
+ CASE WHEN last IN (q1,q2) THEN 1 ELSE 0 END AS points
FROM dbo.users
ORDER BY points DESC;