所以我想在我的查询中添加ORDER BY
,但由于日期字段的格式为2013-02-27 00:00:00
,因此无效。如何动态转换它以便我可以将其与ORDER BY
一起使用?这是我的疑问:
$sql = "SELECT * FROM $wpdb->posts";
$sql .= " INNER JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id";
$sql .= " WHERE $wpdb->posts.post_type = 'task'";
$sql .= " AND $wpdb->postmeta.post_id IN ( SELECT `post_id` FROM $wpdb->postmeta WHERE $wpdb->postmeta.meta_key = '_completed' AND $wpdb->postmeta.meta_value = '0' )";
$sql .= " AND $wpdb->postmeta.post_id IN ( SELECT `post_id` FROM $wpdb->postmeta WHERE $wpdb->postmeta.meta_key = '_assigned' AND $wpdb->postmeta.meta_value = '%s' )";
$sql .= " GROUP BY $wpdb->postmeta.post_id";
$sql .= " ORDER BY $wpdb->postmeta._due ASC";
$tasks = $wpdb->get_results( sprintf( $sql, $user_id ) );
相关字段在表_due
中为wp_postmeta
。这是为Wordpress,但因为它是一个MYSQL问题,我想我会在这里问。谢谢!
答案 0 :(得分:3)
那是因为您正在使用GROUP BY
(错误地)并且MySQL不保证排序的顺序。如果您正确使用该功能,您将获得所需的结果。