H2select表达式列别名
H2 select expression column alias
有H2table:
CREATE TABLE IF NOT EXISTS sometable (ondate DATE NOT NULL);
有数据
INSERT INTO sometable VALUES ('2015-07-07');
INSERT INTO sometable VALUES ('2014-07-07');
INSERT INTO sometable VALUES ('2013-07-07');
我想限制 selected 数据量,但是 以下 select 不起作用 。为什么?
SELECT YEAR(CONVERT(ondate, TIMESTAMP)) AS yr FROM sometable WHERE yr = 2015
错误信息是SELECT YEAR(CONVERT(ondate, TIMESTAMP)) AS yr FROM sometable WHERE yr = 2015;未找到第 "YR" 列; SQL 语句:SELECT YEAR(CONVERT(ondate, TIMESTAMP)) AS yr FROM sometable WHERE yr = 2015 [42122-176] 42S22/42122
您不能在 where 子句中引用列别名。
SELECT
YEAR(CONVERT(ondate, TIMESTAMP)) AS yr
FROM sometable
WHERE YEAR(CONVERT(ondate, TIMESTAMP)) = 2015
错误表明找不到列 yr
,因为 yr
不是您的列名,它只是一个别名。
SQL
不能将别名视为列名 你可以这样写
SELECT
YEAR(CONVERT(ondate, TIMESTAMP)) AS yr
FROM sometable WHERE
YEAR(CONVERT(ondate, TIMESTAMP)) = 2015
这不是特定于 H2 的,这将发生在许多 RDBMS 上。 yr
列实际上不是 SELECT 子句的一部分,而是 CONVERT 语句。如果你想这样做,将整个语句包装在派生的 table 中并查询它,或者使用与你在 WHERE.
中的 SELECT 中相同的子句。
例如;
SELECT * FROM (SELECT YEAR(CONVERT(ondate, TIMESTAMP)) AS yr
FROM sometable) a WHERE a.yr = 2015
或
SELECT YEAR(CONVERT(ondate, TIMESTAMP)) AS yr FROM sometable
WHERE YEAR(CONVERT(ondate, TIMESTAMP)) = 2015
有H2table:
CREATE TABLE IF NOT EXISTS sometable (ondate DATE NOT NULL);
有数据
INSERT INTO sometable VALUES ('2015-07-07');
INSERT INTO sometable VALUES ('2014-07-07');
INSERT INTO sometable VALUES ('2013-07-07');
我想限制 selected 数据量,但是 以下 select 不起作用 。为什么?
SELECT YEAR(CONVERT(ondate, TIMESTAMP)) AS yr FROM sometable WHERE yr = 2015
错误信息是SELECT YEAR(CONVERT(ondate, TIMESTAMP)) AS yr FROM sometable WHERE yr = 2015;未找到第 "YR" 列; SQL 语句:SELECT YEAR(CONVERT(ondate, TIMESTAMP)) AS yr FROM sometable WHERE yr = 2015 [42122-176] 42S22/42122
您不能在 where 子句中引用列别名。
SELECT
YEAR(CONVERT(ondate, TIMESTAMP)) AS yr
FROM sometable
WHERE YEAR(CONVERT(ondate, TIMESTAMP)) = 2015
错误表明找不到列 yr
,因为 yr
不是您的列名,它只是一个别名。
SQL
不能将别名视为列名 你可以这样写
SELECT
YEAR(CONVERT(ondate, TIMESTAMP)) AS yr
FROM sometable WHERE
YEAR(CONVERT(ondate, TIMESTAMP)) = 2015
这不是特定于 H2 的,这将发生在许多 RDBMS 上。 yr
列实际上不是 SELECT 子句的一部分,而是 CONVERT 语句。如果你想这样做,将整个语句包装在派生的 table 中并查询它,或者使用与你在 WHERE.
例如;
SELECT * FROM (SELECT YEAR(CONVERT(ondate, TIMESTAMP)) AS yr
FROM sometable) a WHERE a.yr = 2015
或
SELECT YEAR(CONVERT(ondate, TIMESTAMP)) AS yr FROM sometable
WHERE YEAR(CONVERT(ondate, TIMESTAMP)) = 2015