gpt4 book ai didi

mysql - 显示上传的最后一个图像而不是 mysql group by 中的第一个图像

转载 作者:行者123 更新时间:2023-11-29 18:19:16 26 4
gpt4 key购买 nike

我上传按项目名称分组的图像。

在第一页上,我显示项目名称以及该组中的一个图像。

它显示从该组图像上传的第一张图像,我正在尝试显示最后上传的图像。

我的代码是:

$dbhostname="localhost";  
$dbusername="root";
$dbpassword="";
$db = "main_name";
$dbh = new PDO("mysql:host=$dbhostname;dbname=$db", $dbusername, $dbpassword);

foreach($dbh->query("SELECT *,COUNT(*)
FROM images
GROUP BY project_name ORDER BY img_timestamp DESC") as $row) {
echo "Project Name: " . $row['project_name'];
echo "<br>";
echo $row['imagelink'];
}

供您引用,我的 MySQL 表是:

Table name: images

id project_name imagelink img_timestamp
1 Travier image1 (timestamp of upload)
2 Travier image2 (timestamp of upload)
3 Travier image3 (timestamp of upload)
4 Travier image4 (timestamp of upload)
5 Collosion image1 (timestamp of upload)
6 Collosion image2 (timestamp of upload)
7 Collosion image3 (timestamp of upload)

我们将不胜感激:)

最佳答案

您可以使用 SUBQUERY AND MAX 来实现:

SELECT id, project_name, COUNT(*),
MAX(img_timestamp) AS img_timestamp,
(SELECT imagelink FROM images img WHERE img.project_name = images.project_name ORDER BY img_timestamp DESC LIMIT 1) AS imagelink
FROM images GROUP BY project_name;

请记住,使用 * 将导致两列的名称分别为 img_timestampimagelink,另一种方式可能是使用不同的名称时间戳和图像链接的别名。

关于mysql - 显示上传的最后一个图像而不是 mysql group by 中的第一个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46732702/

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