【问题标题】:php pdo sqlite shows wrong result in sqlfiddle it shows okphp pdo sqlite 在 sqlfiddle 中显示错误结果它显示正常
【发布时间】:2018-03-19 20:02:50
【问题描述】:

晚上好,

当我使用 sqlite 浏览器运行我的代码模拟时,即使 sqlfiddle 显示我的代码工作正常,我也会得到正确的结果。但是当我在 php pdo 中尝试时,它显示错误的结果。

这里是 sqlfiddle http://sqlfiddle.com/#!5/9644d/1

这是我用来调用我的数据的代码

SELECT id,name,type,MIN(price) as price,MAX(best_selling) as best_selling 
FROM Perf 
GROUP BY type,name 
ORDER BY best_selling DESC, id ASC

如果有替代方法可以调用我的代码,它会正常工作。

我的计划是在数据库中获取 best_sales最低价格 的结果,然后按 类型和名称 分组,然后按 排序最畅销首先展示

【问题讨论】:

  • 什么是 PDO 代码,那里的结果是什么?
  • 感谢您的支持 :) 如果您发现它确认了您想要的,请接受答案。如果您对您的问题有细微的改动,那么我会更新答案。

标签: php mysql pdo sqlite


【解决方案1】:

SQLFiddle 中有测试数据 :) 这引起了我的注意 - 谢谢 - 它确实帮助了我 :)

我在这里将所有信息加载到 SQLite 数据库中:) 我使用 SQLite IDE。它工作正常并且出口正常:)

有效的 SQL 查询?

我使用 SQLlite IDE 来检查您的查询。嗯,这不是一个有效的查询;-/ 为什么?

您使用“分组依据”子句。那没问题。但是,每个其他选定的列都必须在一个组函数中才能成为其他 SQL 引擎中的有效 SQL。

考虑到这一点:我决定通过在查询中包含“min”和“max”值来简化查询的调试。

请参阅:http://sqlfiddle.com/#!5/9644d/2,了解我实际使用的有效 SQLite SQL 查询。我也在 PHP 中使用了这个查询。

工作 SQL 查询

所以:这是我使用的查询:

SELECT min(id) AS id_start, 
            max(id) AS id_end, 
            name,
            type,
            MIN(price)            as min_price,
            MAX(price)            as max_price,
            MAX(best_selling) as best_selling 
FROM Perf
GROUP BY type,
         name 
ORDER BY best_selling DESC, 
                 id_end ASC  

结果:

id_start    id_end  name    type    min_price   max_price   best_selling
102 103 another_thing   plastic 10  30  2
101 105 something   glass   20  50  2
100 100 random322312    glass   20  20  1
104 104 something   stone   30  30  1
106 106 random_12321312 glass   111 111 1
107 107 random_12321512 glass   111 111 1
108 108 random_1232312  glass   111 111 1
109 109 random_1236312  glass   111 111 1
97  97  rand55312   glass   20  20  1
98  98  ran62312    glass   20  20  1
99  99  ran44232312 glass   20  20  1

上面的 SQLite IDE 代码工作...

现在我们需要证明,使用 SQLite 数据库的 PDO 版本返回相同的结果...

使用 PDO 将工作 SQL 转换为 PHP...

从这里开始,只需在 IDE 中转换工作 SQL 代码即可 使用 PDO 进入 PHP 中的工作代码。不会那么难吧?

0) 创建到有效 SQLite 数据库的有效 PDO 连接。请参阅下面的导出。

1) 复制 SQL:

    $sql = "SELECT min(id) AS id_start, 
                max(id) AS id_end, 
                name,
                type,
                MIN(price)            as min_price,
                MAX(price)            as max_price,
                MAX(best_selling) as best_selling 
            FROM Perf
            GROUP BY type,
                     name 
            ORDER BY best_selling DESC, 
                    id_end ASC";

    $stmt = $dbPdo->prepare($sql);  
    $stmt->execute();

-----结果---

查看所有结果:

// get them all
$stmt->fetchAll();

结果:

┌──────────────────────────────────────────────────────────────────────────────┐
│                              $stmt->fetchAll()                               │
└──────────────────────────────────────────────────────────────────────────────┘
array (11) [
    array (7) [
        'id_start' => string (3) "102"
        'id_end' => string (3) "103"
        'name' => string (13) "another_thing"
        'type' => string (7) "plastic"
        'min_price' => string (2) "10"
        'max_price' => string (2) "30"
        'best_selling' => string (1) "2"
    ]
    array (7) [
        'id_start' => string (3) "101"
        'id_end' => string (3) "105"
        'name' => string (9) "something"
        'type' => string (5) "glass"
        'min_price' => string (2) "20"
        'max_price' => string (2) "50"
        'best_selling' => string (1) "2"
    ]
    array (7) [
        'id_start' => string (3) "100"
        'id_end' => string (3) "100"
        'name' => string (12) "random322312"
        'type' => string (5) "glass"
        'min_price' => string (2) "20"
        'max_price' => string (2) "20"
        'best_selling' => string (1) "1"
    ]
    array (7) [
        'id_start' => string (3) "104"
        'id_end' => string (3) "104"
        'name' => string (9) "something"
        'type' => string (5) "stone"
        'min_price' => string (2) "30"
        'max_price' => string (2) "30"
        'best_selling' => string (1) "1"
    ]
    array (7) [
        'id_start' => string (3) "106"
        'id_end' => string (3) "106"
        'name' => string (15) "random_12321312"
        'type' => string (5) "glass"
        'min_price' => string (3) "111"
        'max_price' => string (3) "111"
        'best_selling' => string (1) "1"
    ]
    array (7) [
        'id_start' => string (3) "107"
        'id_end' => string (3) "107"
        'name' => string (15) "random_12321512"
        'type' => string (5) "glass"
        'min_price' => string (3) "111"
        'max_price' => string (3) "111"
        'best_selling' => string (1) "1"
    ]
    array (7) [
        'id_start' => string (3) "108"
        'id_end' => string (3) "108"
        'name' => string (14) "random_1232312"
        'type' => string (5) "glass"
        'min_price' => string (3) "111"
        'max_price' => string (3) "111"
        'best_selling' => string (1) "1"
    ]
    array (7) [
        'id_start' => string (3) "109"
        'id_end' => string (3) "109"
        'name' => string (14) "random_1236312"
        'type' => string (5) "glass"
        'min_price' => string (3) "111"
        'max_price' => string (3) "111"
        'best_selling' => string (1) "1"
    ]
    array (7) [
        'id_start' => string (2) "97"
        'id_end' => string (2) "97"
        'name' => string (9) "rand55312"
        'type' => string (5) "glass"
        'min_price' => string (2) "20"
        'max_price' => string (2) "20"
        'best_selling' => string (1) "1"
    ]
    array (7) [
        'id_start' => string (2) "98"
        'id_end' => string (2) "98"
        'name' => string (8) "ran62312"
        'type' => string (5) "glass"
        'min_price' => string (2) "20"
        'max_price' => string (2) "20"
        'best_selling' => string (1) "1"
    ]
    array (7) [
        'id_start' => string (2) "99"
        'id_end' => string (2) "99"
        'name' => string (11) "ran44232312"
        'type' => string (5) "glass"
        'min_price' => string (2) "20"
        'max_price' => string (2) "20"
        'best_selling' => string (1) "1"
    ]
]
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
Called from K:\developer\webapps\testitapp\tests\systest\phpunit\sogQ46632424_sqlite.ph

// ...................... 将使用的导出的 SQLite 数据库:

--
-- File generated with SQLiteStudio v3.1.1 on Mon Oct 9 00:24:55 2017
--
-- Text encoding used: System
--
PRAGMA foreign_keys = off;
BEGIN TRANSACTION;

-- Table: Perf
CREATE TABLE Perf
    ("id" TEXT, "name" TEXT, "type" TEXT, "price" INTEGER, "best_selling" INTEGER);
INSERT INTO Perf (id, name, type, price, best_selling) VALUES ('97', 'rand55312', 'glass', 20, 1);
INSERT INTO Perf (id, name, type, price, best_selling) VALUES ('98', 'ran62312', 'glass', 20, 1);
INSERT INTO Perf (id, name, type, price, best_selling) VALUES ('99', 'ran44232312', 'glass', 20, 1);
INSERT INTO Perf (id, name, type, price, best_selling) VALUES ('100', 'random322312', 'glass', 20, 1);
INSERT INTO Perf (id, name, type, price, best_selling) VALUES ('101', 'something', 'glass', 20, 1);
INSERT INTO Perf (id, name, type, price, best_selling) VALUES ('102', 'another_thing', 'plastic', 10, 1);
INSERT INTO Perf (id, name, type, price, best_selling) VALUES ('103', 'another_thing', 'plastic', 30, 2);
INSERT INTO Perf (id, name, type, price, best_selling) VALUES ('104', 'something', 'stone', 30, 1);
INSERT INTO Perf (id, name, type, price, best_selling) VALUES ('105', 'something', 'glass', 50, 2);
INSERT INTO Perf (id, name, type, price, best_selling) VALUES ('106', 'random_12321312', 'glass', 111, 1);
INSERT INTO Perf (id, name, type, price, best_selling) VALUES ('107', 'random_12321512', 'glass', 111, 1);
INSERT INTO Perf (id, name, type, price, best_selling) VALUES ('108', 'random_1232312', 'glass', 111, 1);
INSERT INTO Perf (id, name, type, price, best_selling) VALUES ('109', 'random_1236312', 'glass', 111, 1);

COMMIT TRANSACTION;
PRAGMA foreign_keys = on;

【讨论】:

  • 谢谢你的答案是我想要的。 +1
猜你喜欢
  • 1970-01-01
  • 2017-02-08
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 2020-04-25
  • 1970-01-01
相关资源
最近更新 更多