我上传按项目名称分组的图片。
在第一页上,我显示一个项目名称,其中包含该组中的一个图像。
它显示从该组图像上传的第一张图片,我正在尝试显示上传的最后一张图片。
我的代码是:
// You must add this Facade in the 'use' section of your file:
use Illuminate\Support\Facades\Input;
return SocialPostResource::collection(
SocialPost::with(['images', 'publication.images'])
->paginate($pagination)
->appends(Input::except('page')) // <- Add this line!
);
供我参考我的MySQL表是:
$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'];
}
帮助将不胜感激:)
答案 0 :(得分:0)
您可以使用A SUBQUERY
和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_timestamp
&amp;的列。 imagelink
另一种方式可能是使用时间戳和imagelink的不同别名。
答案 1 :(得分:0)
尝试以下查询,两个查询都正常运行。
SELECT *
FROM images
WHERE id IN (
SELECT Max(id)
FROM images
GROUP BY project_name
)
OR
SELECT *
FROM images
WHERE timestemp IN (
SELECT Max(timestemp)
FROM images
GROUP BY project_name
)