数据库查询 - 按两个值排序(嵌套订单)

时间:2015-04-07 22:03:44

标签: mysql database wordpress database-design database-schema



我想按两个数据库值排序结果: rating_full ,然后是 rating_count

目前,我按最高 rating_full 排序。它工作正常。

$sql .= " LEFT JOIN {$wpdb->postmeta} rating ON ({$wpdb->posts}.ID = rating.post_id AND rating.meta_key IN ('rating_full'))";

...

...

$sql = "cast(rating.meta_value as decimal(10,2)) {$order}";

...

第一行代码(SELECT语句的一部分)检索 rating_full 部分

第二行代码是ORDER BY部分,目前只使用 rating_count

据我所知,第二行代码中提到的 rating.meta_value rating_full

我试图通过 rating_full rating_count 进行订购 我不确定如何修改第一行以便我可以实现这一点。


感谢

完整代码:

<?php

// Sorting
add_filter('posts_join', 'directorySortingJoin',10,2);
function directorySortingJoin($join, $query) {
global $wpdb, $aThemeOptions;
if ($query->is_main_query() && !$query->is_admin &&   ((isset($_GET['dir-search'])) || (isset($query->query_vars["a-dir-item-category"])) || (isset($query->query_vars["a-dir-item-location"])))) {
    $sql = "";
    // default ordering
    $orderby = (isset($aThemeOptions->directory->defaultOrderby)) ? $aThemeOptions->directory->defaultOrderby : 'post_date';
    // get from get parameters
    if (!empty($_GET['orderby'])) {
        $orderby = $_GET['orderby'];
    }
    if ($orderby == 'rating') {
        $sql .= " LEFT JOIN {$wpdb->postmeta} rating ON   ({$wpdb->posts}.ID = rating.post_id AND rating.meta_key IN ('rating_full'))";

        //$sql .= " LEFT JOIN {$wpdb->postmeta} rating ON (wp_posts.ID = rating.post_id AND rating.meta_key IN ('rating_full')) LEFT JOIN {$wpdb->postmeta} count ON ({$wpdb->posts} = count.post_id AND count.meta_key IN ('rating_count'))";


    }
    if ($orderby == 'packages') {
        directorySaveUserPackagesToDb();
        $sql .= " LEFT JOIN {$wpdb->usermeta} packages ON ({$wpdb->posts}.post_author = packages.user_id AND packages.meta_key IN ('dir_package'))";
    }
    if (isset($aThemeOptions->directory->showFeaturedItemsFirst)) {
        $sql .= " LEFT JOIN {$wpdb->postmeta} featured ON ({$wpdb->posts}.ID = featured.post_id AND featured.meta_key IN ('dir_featured'))";
    }
    $join .= $sql;
    //echo $join;
}
return $join;

  }
   add_filter('posts_orderby', 'directorySortingOrderby',10,2);
  function directorySortingOrderby($orderby, $query) {
    global $wpdb, $aThemeOptions;
 if ($query->is_main_query() && !$query->is_admin && ((isset($_GET['dir-search'])) || (isset($query->query_vars["a-dir-item-category"])) || (isset($query->query_vars["a-dir-item-location"])))) {
    $sql = "";
    // default ordering
    $orderby = (isset($aThemeOptions->directory->defaultOrderby)) ? $aThemeOptions->directory->defaultOrderby : 'post_date';
    $order = (isset($aThemeOptions->directory->defaultOrder)) ?   $aThemeOptions->directory->defaultOrder : 'DESC';
    // get from get parameters
    if (!empty($_GET['orderby'])) {
        $orderby = $_GET['orderby'];
    }
    if (!empty($_GET['order'])) {
        $order = $_GET['order'];
    }
    if ($orderby == 'rating') {
        if (isset($aThemeOptions->directory->showFeaturedItemsFirst)) {
            $sql = "featured.meta_value DESC, convert(rating.meta_value, decimal) {$order}";
        } else {
            //$sql = "convert(rating.meta_value, decimal)    {$order}";

            $sql = "cast(rating.meta_value as   decimal(10,2)) {$order}";
            //$sql = "cast(rating.meta_value as decimal(10,2)) {$order}, count.meta_value {$order}";

        }
    } else if ($orderby == 'packages') {
        if (isset($aThemeOptions->directory->showFeaturedItemsFirst)) {
            $sql = "featured.meta_value DESC, packages.meta_value {$order}";
        } else {
            $sql = "packages.meta_value {$order}";
        }
    } else {
        if (isset($aThemeOptions->directory->showFeaturedItemsFirst)) {
            $sql = "featured.meta_value DESC, {$wpdb->posts}.{$orderby} {$order}";
        }
    }
    $orderby = $sql;
    //echo $orderby;
}
return $orderby;
}
// Save directory packages for sorting
function directorySaveUserPackagesToDb() {
$users = get_users();
// capabilities list
$roles = array(
    'administrator' => 10,
    'directory_5' => 9,
    'directory_4' => 8,
    'directory_3' => 7,
    'directory_2' => 6,
    'directory_1' => 5,
    'editor' => 4,
    'author' => 3,
    'contributor' => 2,
    'subscriber' => 1
);
foreach ($users as $user) {
    if (isset($user->roles[0])) {
        if (array_key_exists($user->roles[0], $roles)) {
            update_user_meta($user->ID, 'dir_package',    $roles[$user->roles[0]]);
        } else {
            update_user_meta($user->ID, 'dir_package',   0);
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

ORDER BY的{​​{3}}在这里应该会有所帮助,尤其是关于排序多列的最后一段。

您的ORDER BY应该是

ORDER BY rating_full DESC, rating_count DESC