-3

如何在 where 中使用 (AS) TOTALNAME

SELECT 
   *, CITY + ' / ' + NAME AS TOTALNAME
FROM 
   FARKETMEZ
WHERE
   TOTALNAME like '%TURKEY%'
4

2 回答 2

2

您不能使用与子句ALIAS处于同一级别的 that 。WHERE

你有两个选择:

  • 通过直接使用表达式

询问:

SELECT  *, CITY + ' / ' + NAME AS TOTALNAME
FROM  FARKETMEZ
WHERE TOTALNAME like '%TURKEY%'
  • 通过包装在子查询中

询问:

SELECT *
FROM
(    SELECT  *, CITY + ' / ' + NAME AS TOTALNAME
    FROM  FARKETMEZ
) a
WHERE TOTALNAME like '%TURKEY%'

SQL 操作顺序如下:

  • FROM 子句
  • WHERE 子句
  • GROUP BY 子句
  • HAVING 子句
  • 选择子句
  • ORDER BY 子句

之所以不能直接使用,是因为子句在创建语句WHERE之前先执行。SELECTALIAS

于 2013-04-30T15:35:50.813 回答
2

您不能直接使用派生表,也可以使用 CTE (SQL Server 2005+):

派生表:

SELECT *
FROM (  SELECT 
           *, CITY + ' / ' + NAME AS TOTALNAME
        FROM 
           FARKETMEZ
      ) AS T
WHERE TOTALNAME like '%TURKEY%'

热电偶:

;WITH CTE AS
(
    SELECT 
           *, CITY + ' / ' + NAME AS TOTALNAME
    FROM 
       FARKETMEZ
)
SELECT *
FROM CTE 
WHERE TOTALNAME like '%TURKEY%'
于 2013-04-30T15:37:07.137 回答