我經常遇到列名沖突,例如以下人工示例:
with
weekdays('name') as (
values
('Monday')
, ('Tuesday')
, ('Wednesday')
, ('Thursday')
, ('Friday')
, ('Saturday')
, ('Sunday')
)
select
substr("name",1,3) as "name"
from weekdays
where 1==1
and "name" == 'Monday'
-- how do I reference current scope's name, where "name" == 'Mon'?
在bash中它將是:
and ./"name" == 'Monday'
參考當前范圍的name而不是首先name從$PATH.
有沒有辦法在 SQLite 中參考當前范圍?
uj5u.com熱心網友回復:
標準 SQL 不允許在WHERE子句中使用派生列的別名,因為SELECT在WHERE.
SQLite 允許它(可能通過用WHERE其各自的運算式替換子句中的別名),但是當與參與表/ctes 的列發生名稱沖突時,別名列會被現有列隱藏。
這意味著在您的情況下,您不能直接參考別名列。
而是重復運算式:
and substr("name",1,3) = 'Mon'
或者,使用子查詢:
select *
from (
select
substr("name",1,3) as "name"
from weekdays
)
where 1=1
and "name" = 'Mon'
或者,更好的是,使用不同的非沖突別名。
請參閱演示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/511645.html
