使用mysql获取最旧的日期时间值 [英] Get the oldest datetime value with mysql
本文介绍了使用mysql获取最旧的日期时间值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个 mySQL 表
I have this mySQL-table
CREATE TABLE `products_to_categories` (
`products_id` int(11) NOT NULL,
`categories_id` int(11) NOT NULL,
`date_added` datetime NOT NULL,
PRIMARY KEY (`products_id`,`categories_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
products_id 2085 有三个条目,因为它属于三个类别.
The products_id 2085 has three entries because it is in three categories.
products_id categories_id date_added
2085 204 2013-03-01
2085 143 2013-03-13
2085 86 2013-03-25
实际上我有这个查询来按日期添加
Actually I have this query to order them by date_added
SELECT categories_id,
products_id
FROM products_to_categories
WHERE categories_id != 0
ORDER BY date_added
如何通过查询获取最旧的值(在本例中为 2013-03-01)?
How do I get the oldest value (in this case 2013-03-01) with a query?
推荐答案
如果您的条目是基于日期的,那么您可以使用 Min
运算符
If you are entries are date based then you can use Min
operator
SELECT products_id,MIN(date_added) AS OldDate
FROM products_to_categories group by products_id
上面的查询将为所有 products_id 选择日期的最小值..
The above query will select the min of date for all products_id..
这篇关于使用mysql获取最旧的日期时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文