SQL Use alias in Where statement

前端 未结 10 1273
南笙
南笙 2020-11-29 06:09

I wounder how I could use an alias in a where statement.

Example :

SELECT SUBSTRING(Column1, 1, 4) + SUBSTRING(Column1, 4, 3)  AS Col1
FROM MyTable
W         


        
10条回答
  •  眼角桃花
    2020-11-29 06:52

    With PostgreSQL 9.3+ OR Oracle 12c, there is now lateral join that allows creating an alias.

    Lateral joins are joints inside witch you can reference preceding tables.

    SELECT col1, col2,col3
    FROM MyTable m
    JOIN LATERAL (
        SELECT SUBSTRING(m.Column1, 1, 4) + SUBSTRING(Column1, 4, 3)  AS Col1 
    ) x ON true
    WHERE Col1 = 'MySearch'
    

    With this syntax, you don't have to use '*' that can be non-performing or recopy all the columns.

提交回复
热议问题