PDO SQLite是否支持数学运算的where子句?

问题描述 投票:0回答:1

首先,对这个问题的措词感到抱歉。

我在SQLite数据库中具有下表Testing

| id | name  | something_numerical |
|----|-------|---------------------|
| 1  | test1 | 100                 |
| 2  | test2 | 500                 |
| 3  | test3 | 1000                |
| 4  | test4 | 5000                |
| 5  | test5 | 10000               |
| 6  | test6 | 25000               |

所有查询均从7.3.12-1+0~20191128.49+debian9~1.gbp24559b执行

以下查询中使用的别名(某种程度上似乎是问题)

something_numeric+100 AS modified_column

按预期通过PDO 占位符works查询并返回第3和4行。

SELECT *, something_numerical+100 AS modified_column 
FROM Testing 
WHERE modified_column BETWEEN 750 AND 7500

通过PDO with占位符不返回任何行查询且没有错误。 (我也尝试使用?进行参数转换,结果相同)

SELECT *, something_numerical+100 AS modified_column 
FROM Testing 
WHERE modified_column BETWEEN :min_modified_column AND :max_modified_column

$parameters = [
   ':min_modified_column'=>750,
   ':max_modified_column'=>7500
];

+100列中没有something_numerical,两个查询均按预期工作,并返回3和4。

这里是SQLite数据库,可帮助进行复制(如有必要)

DROP TABLE IF EXISTS "Testing";
CREATE TABLE "Testing" (
  "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
  "name" text NOT NULL,
  "something_numerical" integer NOT NULL
);

INSERT INTO "Testing" ("id", "name", "something_numerical") VALUES (1,  'test1',    100);
INSERT INTO "Testing" ("id", "name", "something_numerical") VALUES (2,  'test2',    500);
INSERT INTO "Testing" ("id", "name", "something_numerical") VALUES (3,  'test3',    1000);
INSERT INTO "Testing" ("id", "name", "something_numerical") VALUES (4,  'test4',    5000);
INSERT INTO "Testing" ("id", "name", "something_numerical") VALUES (5,  'test5',    10000);
INSERT INTO "Testing" ("id", "name", "something_numerical") VALUES (6,  'test6',    25000);

感谢您的回答。

php sqlite pdo
1个回答
0
投票

您不能在from子句的where子句中使用别名的列,并且无论您使用的是哪种客户端,查询都将引发语法错误。您需要改为重复该表达式:

SELECT t.*, something_numerical+100 AS modified_column 
FROM Testing t
WHERE something_numerical + 100 BETWEEN 750 AND 7500
© www.soinside.com 2019 - 2024. All rights reserved.