我们分 3 次输入 6 条记录。 (早上2点,下午2点,晚上2点)
我尝试了这个 sql 查询,但没有成功。
select c.currency, c.date, c.textbody
from currencies c
inner join (
select currency, max(date) as MaxDate
from currencies
group by currency
) cc on c.currency = cc.currency and c.date = cc.MaxDate
where date ='2017-11-20'
group by currency
order by currency'
需求的故事 每天用户在 3 个时段输入 6 条记录,当早上时间用户输入 2 条记录(EURUSD 和 GBPUSD)。然后我将在网页中显示为一组。然后在晚上用户输入另外 2 条记录(EURUSD 和 GBPUSD),之后我需要显示最新的 2 条记录在顶部组和前 2 条记录和下方。每天6条记录3组。
最佳答案
进行简单查询:
SELECT currency, textBody FROM currencies
WHERE date = '2017-11-20'
ORDER BY timestamp, currency
然后取数据到$rows
数组,添加勾选只显示成对的行并显示。在 PHP 中,这将如下所示:
$rowsCount = count($rows);
if (in_array($rowsCount, [2, 4, 6])) {
for ($i = 0; $i < $rowsCount; $rowIndex++) {
echo $rows[$i]['currency'] . ' ' . $rows[$i + 1]['currency'] . ' '
. $rows[$i]['textbody'] . ' ' . $rows[$i + 1]['textbody'].PHP_EOL;
$rowIndex += 1;
}
}
关于php - sql中单独组的最新结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47396735/