【问题标题】:Php mysql order by datetime descphp mysql 按日期时间顺序排列
【发布时间】:2013-10-28 21:58:41
【问题描述】:

帖子图表;

ID           post_date 
1      2013-10-20:01:00:00
6      2013-09-20:01:00:00

term_relationships 图表;

object_id    term_taxonomy_id    term_order

1               1                    0
1               2                    0
1               3                    0
6               1                    0
6               1                    0

term_taxonomy 图表;

term_taxonomy_id     term_id    taxonomy    parent
1                     1         category      0
2                     2         post_tag      0
3                     3         post_tag      0
4                     4         category      1

--

$post = $wpdb->get_results("SELECT `p`.`ID`, MAX(p.post_date) as `datetime`, `p`.`post_date`, `p`.`post_content`, `p`.`post_title`, `p`.`post_status`, `p`.`post_name`, `p`.`comment_count`, `tax`.`term_taxonomy_id`, `tax`.`term_id`, `tax`.`taxonomy`, `tax`.`parent`, `rel`.`object_id`, `rel`.`term_taxonomy_id`

FROM (`$wpdb->posts` AS p)

INNER JOIN `$wpdb->term_relationships` AS rel ON `rel`.`object_id` = `p`.`ID`

INNER JOIN `$wpdb->term_taxonomy` AS tax ON `tax`.`term_taxonomy_id` = `rel`.`term_taxonomy_id`

WHERE `tax`.`taxonomy` =  'category'AND `p`.`post_status` = 'publish' AND `p`.`post_type` =  'post'

GROUP BY tax.parent

ORDER BY datetime DESC LIMIT 4

");  

tax.parent = 顶级类别 ID post_date 类型数据类型

ORDER BY datetime DESC = 排序不正确 :(

【问题讨论】:

  • 如果您的代码有问题,请发布结果订单和您期望的订单。
  • 我也不知道最后两行应该说什么。
  • 结果由; 11.10.2013 11.10.2013 20.10.2013 ..
  • 请将其添加到问题本身。评论区看不懂。
  • 问题:order by datetime desc 但是; group by tax.parent = 我正在删除改善问题但是;按 tax.parent 分组并按数据时间排序 = 他们是问题所在

标签: php mysql wordpress datetime


【解决方案1】:
    select l.* 
    from table l
    inner join (
      select 
        m_id, max(timestamp) as latest 
      from table 
      group by m_id
    ) r
      on l.timestamp = r.latest and l.m_id = r.m_id
    order by timestamp desc

print your query and enter on phpmyadmin. we get exactly error . so you know easily what you want change

【讨论】:

  • OP 正在使用 WordPress。无论其潜在优点如何,这都可能使您当前的答案出错。
【解决方案2】:

尝试使用表别名 :: OREDER BY p.post_date DESC

【讨论】:

    猜你喜欢
    • 2012-08-16
    • 2011-12-01
    • 2021-11-19
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多