[英]MySQL group by value and max date
I need to extract the last character of columns result_1 and result_2 and group them so I have a list from 0 to 9 indicating when it was generated. 我需要提取result_1和result_2列的最后一个字符并将它们分组,所以我有一个从0到9的列表,指示何时生成。 In the folowing code I managed to obtain a list from 0 to 9 but the date is the earliest date in the table and not the latest. 在下面的代码中,我设法获得了一个从0到9的列表,但是日期是表中最早的日期,而不是最晚的日期。
$sql = "SELECT * FROM ((SELECT RIGHT(result_1, 1) AS results, date
FROM act_results ORDER BY date ASC)
UNION ALL (SELECT RIGHT(result_2, 1) AS results, date
FROM act_results)) s WHERE results != ''";
$sql .= " GROUP BY results";
$table = mysql_query($sql);
while ($row = mysql_fetch_array($table)) {
echo $row['results']." (".$row['date'].") <br>";
}
Any ideas how to fix it? 任何想法如何解决? Thanks in advance. 提前致谢。
Use group by
with max()
: group by
max()
使用group by
:
SELECT results, max(date)
FROM ((SELECT RIGHT(result_1, 1) AS results, date
FROM act_results
) UNION ALL
(SELECT RIGHT(result_2, 1) AS results, date
FROM act_results
)
) s
WHERE results <> ''
GROUP BY results;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.