所以,我知道这是一个非常普通的错误,但是我没有更好的线索来了解查询中发生的情况,也不能比这更好地解释它,这要归功于MySQL并未给出**** UX,请多多包涵。
我正在使用PHP 7.2.1和MySQL 5.7。
我有这个查询:
.bat
它在Navicat或Heidi上工作正常,并向我返回正好1个结果。但是当我在PHP上执行时,mysqli-> query返回空行:
SELECT
sp.*
FROM
`status` s
LEFT JOIN status_pedido sp ON sp.status_pedido_id = s.status_pedido_id
WHERE
s.status_id = (
SELECT
`status`.status_id
FROM
pedido_item_status p1
LEFT JOIN `status` ON `status`.status_id = p1.status_id
LEFT JOIN pedido_item ON pedido_item.pedido_item_id = p1.pedido_item_id
INNER JOIN ( SELECT MAX( pi.cadastrado ) AS maxcadastro FROM pedido_item_status pi GROUP BY pi.pedido_item_id ) p2 ON ( p1.cadastrado = p2.maxcadastro )
AND p1.excluido IS NULL
WHERE
p1.pedido_id = 15720
ORDER BY
`status`.sta_ordem ASC
LIMIT 1
)
Navicat上的EXPLAIN返回以下内容:
object(mysqli_result)#135 (5) { ["current_field"]=> int(0) ["field_count"]=> int(11) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) }
在PHP上,它返回:
+----+-------------+-------------+--------+-------------------+-------------+---------+---------------------------+-------+---------------------------------------------------------------------+--+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | |
+----+-------------+-------------+--------+-------------------+-------------+---------+---------------------------+-------+---------------------------------------------------------------------+--+
| 1 | PRIMARY | s | const | PRIMARY | PRIMARY | 4 | const | 1 | | |
| 1 | PRIMARY | sp | const | PRIMARY | PRIMARY | 4 | const | 1 | | |
| 2 | SUBQUERY | p1 | ref | idx_1,idx_2,idx_3 | idx_1 | 10 | const,const | 5 | Using index condition; Using where; Using temporary; Using filesort | |
| 2 | SUBQUERY | status | eq_ref | PRIMARY | PRIMARY | 4 | emgraf2.p1.status_id | 1 | | |
| 2 | SUBQUERY | pedido_item | eq_ref | PRIMARY | PRIMARY | 4 | emgraf2.p1.pedido_item_id | 1 | Using index | |
| 2 | SUBQUERY | <derived3> | ref | <auto_key0> | <auto_key0> | 5 | emgraf2.p1.cadastrado | 10 | Using index | |
| 3 | DERIVED | pi | ALL | idx_2,idx_3 | | | | 18750 | Using temporary; Using filesort | |
+----+-------------+-------------+--------+-------------------+-------------+---------+---------------------------+-------+---------------------------------------------------------------------+--+
据我从类似的问题中了解到,这与以下事实有关:客户端保留了连接并在分析了整个查询之后执行了查询,而PHP仅在内部WHERE似乎返回0行时才停止。但是我无法弄清楚如何在查询中解决该问题,因为我没有在SQL上设置任何变量,只是进行子查询。
还有另一个问题:完全相同的查询在另一个代码块上运行,我不明白为什么会这样。这是有问题的代码:
array(10) {
["id"]=>
string(1) "1"
["select_type"]=>
string(7) "PRIMARY"
["table"]=>
NULL
["type"]=>
NULL
["possible_keys"]=>
NULL
["key"]=>
NULL
["key_len"]=>
NULL
["ref"]=>
NULL
["rows"]=>
NULL
["Extra"]=>
string(51) "Impossible WHERE noticed after reading const tables"
}
array(10) {
["id"]=>
string(1) "2"
["select_type"]=>
string(8) "SUBQUERY"
["table"]=>
string(2) "p1"
["type"]=>
string(3) "ref"
["possible_keys"]=>
string(17) "idx_1,idx_2,idx_3"
["key"]=>
string(5) "idx_1"
["key_len"]=>
string(2) "10"
["ref"]=>
string(11) "const,const"
["rows"]=>
string(1) "5"
["Extra"]=>
string(67) "Using index condition; Using where; Using temporary; Using filesort"
}
array(10) {
["id"]=>
string(1) "2"
["select_type"]=>
string(8) "SUBQUERY"
["table"]=>
string(6) "status"
["type"]=>
string(6) "eq_ref"
["possible_keys"]=>
string(7) "PRIMARY"
["key"]=>
string(7) "PRIMARY"
["key_len"]=>
string(1) "4"
["ref"]=>
string(20) "emgraf2.p1.status_id"
["rows"]=>
string(1) "1"
["Extra"]=>
NULL
}
array(10) {
["id"]=>
string(1) "2"
["select_type"]=>
string(8) "SUBQUERY"
["table"]=>
string(11) "pedido_item"
["type"]=>
string(6) "eq_ref"
["possible_keys"]=>
string(7) "PRIMARY"
["key"]=>
string(7) "PRIMARY"
["key_len"]=>
string(1) "4"
["ref"]=>
string(25) "emgraf2.p1.pedido_item_id"
["rows"]=>
string(1) "1"
["Extra"]=>
string(11) "Using index"
}
array(10) {
["id"]=>
string(1) "2"
["select_type"]=>
string(8) "SUBQUERY"
["table"]=>
string(10) "<derived3>"
["type"]=>
string(3) "ref"
["possible_keys"]=>
string(11) "<auto_key0>"
["key"]=>
string(11) "<auto_key0>"
["key_len"]=>
string(1) "5"
["ref"]=>
string(21) "emgraf2.p1.cadastrado"
["rows"]=>
string(2) "10"
["Extra"]=>
NULL
}
array(10) {
["id"]=>
string(1) "3"
["select_type"]=>
string(7) "DERIVED"
["table"]=>
string(2) "pi"
["type"]=>
string(3) "ALL"
["possible_keys"]=>
string(11) "idx_2,idx_3"
["key"]=>
NULL
["key_len"]=>
NULL
["ref"]=>
NULL
["rows"]=>
string(5) "18750"
["Extra"]=>
string(31) "Using temporary; Using filesort"
}
这是工作代码:
$sql_sta = "SELECT pis.pedido_item_status_id, pi.pedido_item_id, pi.status_id, p.pedido_id, psp.pedido_status_pedido_id, sp.stp_ordem
FROM pedido_item_status pis
LEFT JOIN pedido_item pi ON pis.pedido_item_id = pi.pedido_item_id
LEFT JOIN pedido p ON pi.pedido_id = p.pedido_id
LEFT JOIN status_pedido sp ON sp.status_pedido_id = p.status_pedido_id
LEFT JOIN pedido_status_pedido psp ON (psp.status_pedido_id = sp.status_pedido_id AND psp.pedido_id = p.pedido_id)
WHERE pis.pedido_item_status_id = {$_GET['del']}
LIMIT 1";
$res_sta = ClassDb::query($sql_sta);
$status_del = $res_sta->fetch_assoc();
$sql_ped = "SELECT sp.* FROM `status` s
LEFT JOIN status_pedido sp ON sp.status_pedido_id = s.status_pedido_id
WHERE s.status_id = (
SELECT `status`.status_id FROM pedido_item_status p1
LEFT JOIN `status` ON `status`.status_id = p1.status_id
LEFT JOIN pedido_item ON pedido_item.pedido_item_id = p1.pedido_item_id
INNER JOIN ( SELECT pi.pedido_id, MAX( pi.cadastrado ) AS maxcadastro FROM pedido_item_status pi GROUP BY pi.pedido_item_id ) p2
ON ( p1.cadastrado = p2.maxcadastro ) AND p1.excluido IS NULL
WHERE p1.pedido_id = 15720
ORDER BY `status`.sta_ordem ASC
LIMIT 1
)";
$res_ped = ClassDb::query($sql_ped);
它们之间的唯一区别是我首先运行一个不同的查询以检索$sql_sta = "SELECT sp.* FROM pedido_item pi
LEFT JOIN pedido p ON pi.pedido_id = p.pedido_id
LEFT JOIN status_pedido sp ON sp.status_pedido_id = p.status_pedido_id
WHERE pi.pedido_item_id = {$_POST['pedido_item_id']}";
$res_sta = ClassDb::query($sql_sta);
$status_antigo = $res_sta->fetch_assoc();
$sql_ped = "SELECT sp.* FROM `status` s
LEFT JOIN status_pedido sp ON sp.status_pedido_id = s.status_pedido_id
WHERE s.status_id = (
SELECT `status`.status_id FROM pedido_item_status p1
LEFT JOIN `status` ON `status`.status_id = p1.status_id
LEFT JOIN pedido_item ON pedido_item.pedido_item_id = p1.pedido_item_id
INNER JOIN ( SELECT MAX( pi.cadastrado ) AS maxcadastro FROM pedido_item_status pi GROUP BY pi.pedido_item_id ) p2
ON ( p1.cadastrado = p2.maxcadastro ) AND p1.excluido IS NULL
WHERE p1.pedido_id = {$_POST['pedido_id']}
ORDER BY `status`.sta_ordem ASC
LIMIT 1
)";
$res_ped = ClassDb::query($sql_ped);
$status_atual = $res_ped->fetch_assoc();
,我试图将变量更改为静态值并注释第一个查询,输出仍然相同,并且,工作代码在简单的POST之后运行,在我插入一行之后,而有问题的从ajax GET请求运行,在我更新一行以软删除它之后(将列pedido_id
设置为NOW(),请考虑作为Laravel的excluido
)。尝试在Navicat上手动运行查询,效果很好。
由于帖子已经太长,并且表又大又复杂,所以只要假设结果在那里,所有列都存在,查询就可以在Navicat上很好地执行并返回结果,并且在工作中也返回结果具有相同查询的代码块。
Edit1:没有PHP错误或异常,也没有MYSQLI错误,我已经将它们设置为显示。查询只是返回空结果,代码正常运行。
编辑2:尝试直接在MySQL Shell上运行,结果与Navicat或Heidi相同:
deleted_at
答案 0 :(得分:0)
好吧,我刚刚发现了“错误”的原因。实际上,这根本不是一个错误,只是我很愚蠢。
在PHP代码上,我更新了一行以将excluido
设置为NOW()
,因此选中的行之一被软删除,这导致查询返回0行,但是由于自动提交被打开了到FALSE,当我转储查询时,我没有提交查询,更新被回滚,并且我在Navicat上运行查询,而没有任何软删除的行,这就是它起作用的原因。现在,在调试之前提交查询,该行被软删除,并且Navicat / Heidi也返回0行。
我仍然没有弄清楚为什么它返回0行,但是我认为这个问题属于另一个问题,而不是这个问题,所以如果我不能解决问题,我将提出一个新问题。我。我只是在回答自己的问题以关闭此线程,而不会浪费您的宝贵时间。
感谢所有尝试在评论中帮助我的人。