在 SQL 服务器中搜索 'AS' 别名
Search 'AS' alias in SQL Server
我有一个 sql 查询,它根据销售 table 中的客户 ID 从客户 table 中提取客户全名并将信息绑定到 asp.net 列表视图。我现在想通过客户全名搜索记录。我使用了下面的查询,但它一直告诉我列 "Fullname" 是无效的列名。我该如何着手修改此查询以使其生效?
SELECT
tbl_Sales.SaleID, tbl_Sales.CustomerID,
(SELECT Firstname + ' ' + Lastname
FROM tbl_Customers
WHERE CustomerID = tbl_Sales.CustomerID) AS CustomerName,
tbl_Sales.Price
FROM
tbl_Sales
WHERE
CustomerName LIKE '%John%'
提前致谢。
您不能在同一个 select 语句中使用 Alias name
,因为 where
将在 select
之前处理。
将原始查询设为 sub select
并在 where clause
中使用 alias name
。试试这个。
SELECT *
FROM (SELECT tbl_Sales.SaleID,
tbl_Sales.CustomerID,
(SELECT Firstname + ' ' + Lastname
FROM tbl_Customers
WHERE CustomerID = tbl_Sales.CustomerID) AS CustomerName,
tbl_Sales.Price
FROM tbl_Sales) A
WHERE CustomerName LIKE '%John%'
或使用Cross apply
SELECT tbl_Sales.SaleID,
tbl_Sales.CustomerID,
t.CustomerName,
tbl_Sales.Price
FROM tbl_Sales
CROSS apply (SELECT Firstname + ' ' + Lastname AS CustomerName
FROM tbl_Customers
WHERE CustomerID = tbl_Sales.CustomerID) t
WHERE t.CustomerName LIKE '%John%'
您不能在 WHERE 子句中传递 ALIAS name 而不是您应该使用 JOIN
试试这个:
SELECT S.SaleID, S.CustomerID, (C.Firstname + ' ' + C.Lastname) AS CustomerName, S.Price
FROM tbl_Sales S
INNER JOIN tbl_Customers C ON S.CustomerID = C.CustomerID
WHERE (C.Firstname + ' ' + C.Lastname) LIKE '%John%'
您可以像这样检查每一列,而不是检查组合值。合并列并比较不是 SARGable,并且会导致性能不佳:
SELECT
tbl_Sales.SaleID,
tbl_Sales.CustomerID,
cus.Firstname + ' ' + cus.Lastname CustomerName,
tbl_Sales.Price
FROM
tbl_Sales
JOIN
tbl_Customers cus
ON
cus.CustomerID = tbl_Sales.CustomerID
WHERE
(cus.Firstname LIKE '%John%' or
cus.Lastname LIKE '%John%')
我有一个 sql 查询,它根据销售 table 中的客户 ID 从客户 table 中提取客户全名并将信息绑定到 asp.net 列表视图。我现在想通过客户全名搜索记录。我使用了下面的查询,但它一直告诉我列 "Fullname" 是无效的列名。我该如何着手修改此查询以使其生效?
SELECT
tbl_Sales.SaleID, tbl_Sales.CustomerID,
(SELECT Firstname + ' ' + Lastname
FROM tbl_Customers
WHERE CustomerID = tbl_Sales.CustomerID) AS CustomerName,
tbl_Sales.Price
FROM
tbl_Sales
WHERE
CustomerName LIKE '%John%'
提前致谢。
您不能在同一个 select 语句中使用 Alias name
,因为 where
将在 select
之前处理。
将原始查询设为 sub select
并在 where clause
中使用 alias name
。试试这个。
SELECT *
FROM (SELECT tbl_Sales.SaleID,
tbl_Sales.CustomerID,
(SELECT Firstname + ' ' + Lastname
FROM tbl_Customers
WHERE CustomerID = tbl_Sales.CustomerID) AS CustomerName,
tbl_Sales.Price
FROM tbl_Sales) A
WHERE CustomerName LIKE '%John%'
或使用Cross apply
SELECT tbl_Sales.SaleID,
tbl_Sales.CustomerID,
t.CustomerName,
tbl_Sales.Price
FROM tbl_Sales
CROSS apply (SELECT Firstname + ' ' + Lastname AS CustomerName
FROM tbl_Customers
WHERE CustomerID = tbl_Sales.CustomerID) t
WHERE t.CustomerName LIKE '%John%'
您不能在 WHERE 子句中传递 ALIAS name 而不是您应该使用 JOIN
试试这个:
SELECT S.SaleID, S.CustomerID, (C.Firstname + ' ' + C.Lastname) AS CustomerName, S.Price
FROM tbl_Sales S
INNER JOIN tbl_Customers C ON S.CustomerID = C.CustomerID
WHERE (C.Firstname + ' ' + C.Lastname) LIKE '%John%'
您可以像这样检查每一列,而不是检查组合值。合并列并比较不是 SARGable,并且会导致性能不佳:
SELECT
tbl_Sales.SaleID,
tbl_Sales.CustomerID,
cus.Firstname + ' ' + cus.Lastname CustomerName,
tbl_Sales.Price
FROM
tbl_Sales
JOIN
tbl_Customers cus
ON
cus.CustomerID = tbl_Sales.CustomerID
WHERE
(cus.Firstname LIKE '%John%' or
cus.Lastname LIKE '%John%')