I have a query that lists theme parks by order of newest news story. The query is below.
$sql2 = 'SELECT tpf_news.park_id, name
FROM tpf_news
INNER JOIN tpf_parks ON tpf_news.park_id = tpf_parks.park_id GROUP BY name ORDER BY date DESC ' ;
$result2 = $pdo->query($sql2);
It was working fine until a theme park got more than one news story, the date being grouped is the older date not the newer so the theme park would remain at its original position, not jumping to the top of the list when it has a new story attached. How can I change the query so the grouping is done by most recent date?
If it's needed, below is the php foreach to display the data.
<?php foreach ($result2 as $row2): ?>
<h4>
<a class="bloglink" href="parknews.php?park_id=<?php echo $row2['park_id'];?>">
<?php echo $row2['name']; ?>
</a>
</h4>
<br>
<?php endforeach; ?>
Thanks
You are not selecting a date, so essentially it will use a "random" date. You could try selecting like this:
SELECT MAX(date) thedate, tpf_news.park_id, name
FROM tpf_news
INNER JOIN tpf_parks ON tpf_news.park_id = tpf_parks.park_id GROUP BY name ORDER BY thedate DESC
This selects the largest date within each group (name) and then orders by those dates.
I just worked it out:
$sql2 = 'SELECT tpf_news.park_id, name
FROM tpf_news
INNER JOIN tpf_parks ON tpf_news.park_id = tpf_parks.park_id GROUP BY name
ORDER BY min(STR_TO_DATE(date, "%Y-%m-%d"))'
That seems to do the job. Thanks
SELECT tpf_news.park_id, name
FROM tpf_news
INNER JOIN tpf_parks ON tpf_news.park_id = tpf_parks.park_id
GROUP BY name ORDER BY date DESC, **(AutoIncrement_Field)** DESC
Hope This Will Help..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.