相关子查询中没有这样的列
No Such Column in Correlated Subquery
所以我写了一个与 this answer 中描述的查询几乎相同的查询。但是我收到一条错误消息:没有这样的列 b.when
.
我的select声明:
SELECT
mileage,
(SELECT b.mileage FROM MileageEvents as b WHERE `b.when` < `a.when` ORDER BY `b.when` DESC LIMIT 1) as last_mileage,
gallons,
cost_per_gallon,
`when`
FROM MileageEvents as a
我知道我以前写过这样的查询,但我似乎无法弄清楚这个查询是怎么回事。我做错了什么?
我的数据库转储:
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "MileageEvents" (
"mileage" INTEGER,
"when" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
"cost_per_gallon" INTEGER,
"gallons" INTEGER,
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"car_id" INTEGER NOT NULL
);
INSERT INTO MileageEvents VALUES(150000,'2019-03-12',3.3500000000000000888,12,1,1);
INSERT INTO MileageEvents VALUES(150300,'2019-03-19',3.25,12,2,1);
INSERT INTO MileageEvents VALUES(150693,'2019-03-22',3.4500000000000001776,12,3,1);
INSERT INTO MileageEvents VALUES(151000,'2019-03-25',3.3900000000000001243,12,4,1);
INSERT INTO MileageEvents VALUES(151600,'2019-04-01',2.25,12,5,1);
INSERT INTO MileageEvents VALUES(151883,'2019-06-10 23:01:43',2.4500000000000001776,11.695999999999999729,6,1);
DELETE FROM sqlite_sequence;
INSERT INTO sqlite_sequence VALUES('MileageEvents',6);
COMMIT;
(首先,我会使用正常的双引号转义而不是 MySQL 样式的反引号,或者更好的是,一个不是保留关键字的标识符)
您需要使用b."when"
。您所做的是将整个字符串 b.when
视为标识符,table 名称和列名称之间没有分隔。
重写:
SELECT
mileage,
(SELECT b.mileage FROM MileageEvents as b WHERE b."when" < a."when" ORDER BY b."when" DESC LIMIT 1) as last_mileage,
gallons,
cost_per_gallon,
"when"
FROM MileageEvents as a;
注意:如果使用 sqlite 3.25 或更新版本,您可以完全避免子查询:
SELECT
mileage,
lag(mileage) OVER (ORDER BY "when") AS last_mileage,
gallons,
cost_per_gallon,
"when"
FROM MileageEvents;
所以我写了一个与 this answer 中描述的查询几乎相同的查询。但是我收到一条错误消息:没有这样的列 b.when
.
我的select声明:
SELECT
mileage,
(SELECT b.mileage FROM MileageEvents as b WHERE `b.when` < `a.when` ORDER BY `b.when` DESC LIMIT 1) as last_mileage,
gallons,
cost_per_gallon,
`when`
FROM MileageEvents as a
我知道我以前写过这样的查询,但我似乎无法弄清楚这个查询是怎么回事。我做错了什么?
我的数据库转储:
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "MileageEvents" (
"mileage" INTEGER,
"when" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
"cost_per_gallon" INTEGER,
"gallons" INTEGER,
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"car_id" INTEGER NOT NULL
);
INSERT INTO MileageEvents VALUES(150000,'2019-03-12',3.3500000000000000888,12,1,1);
INSERT INTO MileageEvents VALUES(150300,'2019-03-19',3.25,12,2,1);
INSERT INTO MileageEvents VALUES(150693,'2019-03-22',3.4500000000000001776,12,3,1);
INSERT INTO MileageEvents VALUES(151000,'2019-03-25',3.3900000000000001243,12,4,1);
INSERT INTO MileageEvents VALUES(151600,'2019-04-01',2.25,12,5,1);
INSERT INTO MileageEvents VALUES(151883,'2019-06-10 23:01:43',2.4500000000000001776,11.695999999999999729,6,1);
DELETE FROM sqlite_sequence;
INSERT INTO sqlite_sequence VALUES('MileageEvents',6);
COMMIT;
(首先,我会使用正常的双引号转义而不是 MySQL 样式的反引号,或者更好的是,一个不是保留关键字的标识符)
您需要使用b."when"
。您所做的是将整个字符串 b.when
视为标识符,table 名称和列名称之间没有分隔。
重写:
SELECT
mileage,
(SELECT b.mileage FROM MileageEvents as b WHERE b."when" < a."when" ORDER BY b."when" DESC LIMIT 1) as last_mileage,
gallons,
cost_per_gallon,
"when"
FROM MileageEvents as a;
注意:如果使用 sqlite 3.25 或更新版本,您可以完全避免子查询:
SELECT
mileage,
lag(mileage) OVER (ORDER BY "when") AS last_mileage,
gallons,
cost_per_gallon,
"when"
FROM MileageEvents;