Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select values with a column alias?

Tags:

sql

sqlite

If I do this in SQLite3:

SELECT *
FROM (VALUES (1), (2), (3)) AS "tbl.col"
WHERE "tbl"."col" = 1

I get: no such column: tbl.col

What is the correct way?

Thanks!

like image 211
Agendum Avatar asked Dec 11 '25 05:12

Agendum


1 Answers

I think you are looking for this:

SELECT *
FROM (VALUES (1), (2), (3)) AS tbl(col)
WHERE tbl.col = 1;

Note that when you escape an identifier (using "tbl.col"), then that is one name that has a period in it. Not two names.

EDIT:

I would have expected the above to work, but it doesn't in SQLite. One alternative is to use a CTE:

with tbl(col) as (
      VALUES (1), (2), (3)
     )
SELECT *
FROM tbl
where tbl.col = 1
like image 93
Gordon Linoff Avatar answered Dec 13 '25 23:12

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!