[英]Max of month year Mysql/PHP
Here is my sql table 这是我的SQL表
| id | data | date |
|----|-----------|----------------|
| 1 | some data | october 2014 |
| 2 | some data | september 2014 |
and I want to fetch the latest month year in the table since date is varchar how do I get the max date out ? 而且我想获取表中的最新月份年份,因为date是varchar如何获取最大日期?
This should do it: 应该这样做:
SELECT max(str_to_date(date, '%M %Y')) as max_date
FROM schema.data;
Here's a fully tested PHP example: 这是一个经过全面测试的PHP示例:
<?php
function getMaxDate() {
try {
$db = new PDO("mysql:host=localhost;charset=utf8", "root", "root");
$cmd = $db->prepare("
SELECT max(str_to_date(date, '%M %Y')) as max_date
FROM schema.data;
");
$cmd->execute();
$result = $cmd->fetch();
return $result[0];
} catch (PDOException $e) { echo $e->getMessage(); return; }
}
echo getMaxDate();
?>
Returns the following: 返回以下内容:
2014-10-00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.