当我使用sqlite浏览器运行我的代码模拟时,即使sqlfiddle显示我的代码工作正常,我也能得到正确的结果。但是当我在php pdo中尝试它时显示错误的结果。
这里是sqlfiddle http://sqlfiddle.com/#!5/9644d/1< ~~在这个链接中工作正常,但是在php pdo中它没有工作......
这是我用来调用我的数据的代码
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_selling 的结果,数据库中可用最低价格,然后按类型和名称分组,然后按排序best_selling 首先显示
答案 0 :(得分:1)
在SQLFiddle中有测试数据:)这引起了我的注意 - 谢谢 - 它确实帮助了我:))
我在这里将所有信息加载到SQLite数据库中:)我使用SQLite IDE。它工作正常并导出ok:)
我使用SQLlite IDE检查您的查询。嗯,这不是一个有效的查询; - /为什么?
您使用'group by'子句。那没问题。但是,所有其他所选列必须在组函数中才能成为其他SQL引擎中的有效SQL。
考虑到这一点:我决定通过在查询中包含“min”和“max”值来更轻松地调试查询。
请参阅:http://sqlfiddle.com/#!5/9644d/2了解我实际使用的有效SQLite SQL查询。我也在PHP中使用了这个查询。
所以:这是我使用的查询:
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数据库的PDO版本返回相同的结果......
从这里开始,只需在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;