SQL: Alias Column Name for Use in CASE Statement

前端 未结 11 1785
悲哀的现实
悲哀的现实 2020-12-04 21:38

Is it possible to alias a column name and then use that in a CASE statement? For example,

SELECT col1 as a, CASE WHEN a = \'test\' THEN \'yes\' END as value          


        
11条回答
  •  长情又很酷
    2020-12-04 21:40

    I think that MySql and MsSql won't allow this because they will try to find all columns in the CASE clause as columns of the tables in the WHERE clause.

    I don't know what DBMS you are talking about, but I guess you could do something like this in any DBMS:

    SELECT *, CASE WHEN a = 'test' THEN 'yes' END as value FROM (
       SELECT col1 as a FROM table
    ) q
    

提交回复
热议问题