gpt4 book ai didi

php - PDO查询错误: 2013 Lost connection to MySQL server during query [Need Optimization Help]

转载 作者:行者123 更新时间:2023-11-30 01:26:54 26 4
gpt4 key购买 nike

我有一个查询,根据添加日期从 mysql 数据库中选择数十行。

查询有效,但经常遇到错误,效率低下。注意:该表有近 10k+ 行。

这是我遇到的问题:

PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2013 Lost connection to MySQL server during query' in somefile.php:49
Stack trace:
#0 somefile.php(49): PDOStatement->execute()

这是导致错误的查询:

// Problematic Query
$query = $conn->prepare('SELECT *
FROM table_name
WHERE DATE(date_added) IN (
SELECT DATE(date_added) as creation_date
FROM table_name
GROUP BY creation_date DESC
)
ORDER BY id DESC
LIMIT :limit OFFSET :offset'
);
$query->bindValue(':limit', (int) trim($entrylimit), PDO::PARAM_INT);
$query->bindValue(':offset', (int) trim($offset), PDO::PARAM_INT);
$query->execute();

对于如何改进此查询有什么建议吗?

编辑:为了希望解释这是在做什么:它收集唯一创建日期的列表,然后使用它来收集具有该特定创建日期的所有行。 (我曾经在 php 循环中完成此操作,但速度非常慢。)

最佳答案

尝试这个查询。

 $query = $conn->prepare('SELECT * , DATE(date_added) as creation_date 
FROM table_name GROUP BY creation_date
ORDER BY id DESC LIMIT :limit OFFSET :offset');

编辑。您正在单独编写 DESC,而没有 Order by 子句

改变这个

  GROUP BY creation_date DESC
^^----//-Missing ORDER BY column

   GROUP BY creation_date 

或按列写下您的订单。

关于php - PDO查询错误: 2013 Lost connection to MySQL server during query [Need Optimization Help],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17902674/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com