MYSQL按日期排序,最大日期

时间:2011-12-16 22:43:38

标签: mysql wordpress date

我正在使用wordpress,我正在尝试按日期自定义字段对某些帖子进行排序。那么我可以按日期对结果进行排序,但我只想显示截至今天的结果。我不想从具有未来日期的帖子中获得结果。

这是我正在使用的代码:

$querystr = "
            SELECT wposts.*
            FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
            WHERE wposts.ID = wpostmeta.post_id
            AND wpostmeta.meta_key = 'date'
            AND wposts.post_status = 'publish'
            AND wposts.post_type = games'
            ORDER BY STR_TO_DATE(wpostmeta.meta_value, '%m/%d/%Y') DESC
            ";

        $pageposts = $wpdb->get_results($querystr, OBJECT);

4 个答案:

答案 0 :(得分:1)

将此添加到where子句:

AND STR_TO_DATE(wpostmeta.meta_value, '%m/%d/%Y') <= DATE(NOW())

答案 1 :(得分:1)

只需将其添加到where子句:

SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
  AND wpostmeta.meta_key = 'date'
  AND wposts.post_status = 'publish'
  AND wposts.post_type = 'games'
  AND STR_TO_DATE(wpostmeta.meta_value, '%m/%d/%Y') < NOW()
ORDER BY STR_TO_DATE(wpostmeta.meta_value, '%m/%d/%Y') ASC

答案 2 :(得分:0)

如果没有看到你的数据行,我会认为这样的事情可以解决问题:

$today = "12/16/2011";

$querystr = "SELECT wposts.*
            FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
            WHERE wposts.ID = wpostmeta.post_id
            AND wpostmeta.meta_key = 'date'
            AND wposts.post_status = 'publish'
            AND wposts.post_type = 'games'
            STR_TO_DATE(wpostmeta.meta_value, '%m/%d/%Y') <= $today;
            ORDER BY STR_TO_DATE(wpostmeta.meta_value, '%m/%d/%Y') ASC";

您在PHP中以与%m /%d /%Y相同的格式生成今天的日期

答案 3 :(得分:-2)

WHERE STR_TO_DATE >= [start DATE] AND STR_TO_DATE <= [END DATE]