从MySQL到SQL Server 2005。

时间:2021-06-16 16:28:22

How do I make this MySQL query into SQL Server equivalents?

如何将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

1 个解决方案

#1


4  

The equivalent to IF in SQL Server is the CASE expression. (Or IIF in SQL Server 2012.) I also removed the aliases which seem largely pointless in this case.

与SQL Server中的IF等价的是CASE表达式。(或SQL Server 2012中的IIF。)我还删除了在本例中看起来毫无意义的别名。

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;

#1


4  

The equivalent to IF in SQL Server is the CASE expression. (Or IIF in SQL Server 2012.) I also removed the aliases which seem largely pointless in this case.

与SQL Server中的IF等价的是CASE表达式。(或SQL Server 2012中的IIF。)我还删除了在本例中看起来毫无意义的别名。

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;