我正在编写一个查询,其中我正在对几个不同的表进行联合,以将它们组合成一个项。
让我们举一个例子,我有一个表Purchases
和一个表Leases
,我正在为Sales
创建一个新的结果。在其中一个表上,我将Description
基于不同的列。例如:
SELECT
u.id as UserId,
p.name as Name,
p.email as Email,
'Purchase' as Table,
p.id as SaleId,
p.status_id as SaleStatusId,
p.description as SaleDescription
FROM users u
INNER JOIN purchase p on u.id = p.user_id
UNION ALL
SELECT
u.id as UserId,
l.name as Name,
l.email as Email,
'Lease' as Table,
l.id as SaleId,
l.status_id as SaleStatusId,
CASE
WHEN l.status_id = 2 THEN 'Finalized Lease'
ELSE 'Pending Lease'
END as SaleDescription
FROM users u
INNER JOIN lease l on u.id = l.user_id
这一切都很好,但是当我试图搜索我添加的描述时,问题就来了。所以我尝试添加where子句,例如:
SELECT
u.id as UserId,
p.name as Name,
p.email as Email,
'Purchase' as Table,
p.id as SaleId,
p.status_id as SaleStatusId,
p.description as SaleDescription
FROM users u
INNER JOIN purchase p on u.id = p.user_id
WHERE (@searchDescription = {} or p.description = any (@searchDescriptions))
UNION ALL
SELECT
u.id as UserId,
l.name as Name,
l.email as Email,
'Lease' as Table,
l.id as SaleId,
l.status_id as SaleStatusId,
CASE
WHEN l.status_id = 2 THEN 'Finalized Lease'
ELSE 'Pending Lease'
END as SaleDescription
FROM users u
INNER JOIN lease l on u.id = l.user_id
WHERE (@searchDescription = {} or SaleDescription = any (@searchDescriptions))
searchDescription
是一个字符串列表,如果有匹配SaleDescription的字符串,我希望它返回。其他人都被忽略了。
所以第一个WHERE
语句可以正常工作,因为它在现有列上搜索。第二个WHERE
不工作,因为它不能搜索一个还没有“被填充”的列,我得到这个错误:There is a column named
SaleDescriptionin table "*SELECT* 1", but it cannot be referenced from this part of the query
所以基本上我想知道我如何才能做到这一点?如何将where
子句添加到由case
select
填充的列?
**注意这是dumby代码,可能会有错误-我还没有测试过它
2条答案
按热度按时间xzv2uavs1#
CTE可用于选择所需的列,然后在CTE上进行过滤,如:
lzfw57am2#
您得到该错误的原因是由于logical [order of] query processing与语法规则要求的子句顺序不同。而“选择查询”根据语法以“选择”开始;它不是要执行的第一个子句。获取数据(FROM & JOIN)然后过滤该数据(WHERE)的子句是要执行的第一组子句。
这对您的问题意味着在select子句下建立的列别名是“未知的”,直到考虑select子句,并且ths在where子句之后。因此,您将获得所遇到的错误类型。(* 注:这个列别名未知的问题对于Postgres和许多其他RDBMS都是正确的,但不是全部。*)
要解决这个问题,有几种方法:
fiddle