문제

I'd like to reuse an alias that contains a non-alphabetic character in a query, something like:

 SELECT 42 AS "the#answer", "the#answer"+8 AS "fifty";

Output I want: 42|50; output I get: 42|8.

I've tried almost every possible combination of quote types, and looked for documentation, but I can't seem to find a working solution.

Any idea?

도움이 되었습니까?

해결책

SQL cannot refer to an alias from the same output clause in which it was introduced. (It has nothing to do with quoting, which only allows otherwise invalid identifiers; some SQL vendors would have thrown an error, but SQLite appears "more relaxed" in the handling of this case.)

You could use a nested query (sqlfiddle).

SELECT fortytwo, fortytwo + 8 as fifty
FROM (
    SELECT 42 AS fortytwo)

This works because the referenced identifier, fortytwo, was introduced in a "previous" output clause.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top