使用mysql获取最旧的日期时间值

编程入门 行业动态 更新时间:2024-10-24 07:35:11
本文介绍了使用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获取最旧的日期时间值

本文发布于:2023-10-27 23:31:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534854.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   时间   mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!