mysql中,某字段的类型是date,现在想从这个字段中截取最大的日期和最小的日期。
例如在PHP页面中,显示出最大日期和最小日期 而MYSQL字段A中有多个日期:2008-11-20、2008-11-19、2008-11-20、2008-10-10 然后可以截取的2008-10-10和2008-11-20,这两个日期
使用MYSQL自带的极限函数即可。 如: select max(字段) from 表; selcet min(字段) from 表; 当然如果你想一步到位,求到最大或最小日期的记录ID号,可以用这个: select id,max(字段) from 表 group by id; selcet id,min(字段) from 表 group by id;
SQL MAX() 实例
我们拥有下面这个 "Orders" 表:
O_Id | OrderDate | OrderPrice | Customer |
---|---|---|---|
1 | 2008/12/29 | 1000 | Bush |
2 | 2008/11/23 | 1600 | Carter |
3 | 2008/10/05 | 700 | Bush |
4 | 2008/09/28 | 300 | Bush |
5 | 2008/08/06 | 2000 | Adams |
6 | 2008/07/21 | 100 | Carter |
现在,我们希望查找 "OrderPrice" 列的最大值。
我们使用如下 SQL 语句:
SELECT MAX(OrderPrice) AS LargestOrderPrice FROM Orders
结果集类似这样:
LargestOrderPrice |
---|
2000 |