【问题标题】:Does PDO SQLite support where clauses on mathematical operations?PDO SQLite 是否支持数学运算的 where 子句?
【发布时间】:2020-05-15 23:56:30
【问题描述】:

首先,我很抱歉问题的措辞。

我在 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 没有占位符按预期工作进行查询并返回第 3 行和第 4 行。

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

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

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
];

没有+100something_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);

感谢您的回答。

【问题讨论】:

  • 如果在$parameters 键中去掉冒号会怎样?

标签: php sqlite pdo


【解决方案1】:

好的,所以这是一个类型转换问题。

将这些参数中的每一个与正确的类型 (PDO::PARAM_INT) 绑定可以解决问题。

$prepared_statement->bindValue(
    ':min_modified_column', 
    750, 
    \PDO::PARAM_INT
);
$prepared_statement->bindValue(
    ':max_modified_column', 
    7500, 
    \PDO::PARAM_INT
);

似乎,没有任何操作 SQLite/PDO 可以从列类型推断类型,但是在对其内容进行操作之后就不能再这样做了。

下面的问题让我走上了正轨

Android/SQLite - Bit operation on WHERE clause

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多