从查询中选择最小/最大元

时间:2013-04-10 作者:steen

有点不知所措了。我对每个帖子都有一个元价格。我想获取此搜索/查询的最小和最大价格元值。目前,它返回所有帖子的最小最大值。可能是个愚蠢的问题,但你如何查询这个查询?或者其他类似的解决方案。

    // get and display lowest price from search query ?
     $query = "SELECT min(cast(meta_value as unsigned)) FROM wp_postmeta WHERE meta_key=\'_wp_price\' AND meta_value IS NOT NULL AND meta_value <> 0";
$the_min = $wpdb->get_var($query);
   echo \'<span class="pricerange">Price range: \'.ft_currency_conv(of_get_option(\'ft_currency\')).\'\'.$the_min.\' to</span>\';

   // get and display highest price from search query how?
    $query = "SELECT max(cast(meta_value as unsigned)) FROM wp_postmeta WHERE meta_key=\'_wp_price\'";
$the_max = $wpdb->get_var($query);
   echo \'<span class="pricerange">\'.ft_currency_conv(of_get_option(\'ft_currency\')).\'\'.$the_max.\'</span>\';

    // the final search query
    $querystr = "
            SELECT DISTINCT $wpdb->posts.ID, $wpdb->posts.* 
            FROM $wpdb->posts
            LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
            LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
            LEFT JOIN $wpdb->terms ON($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id) 
            LEFT JOIN $wpdb->postmeta ON($wpdb->postmeta.post_id = $wpdb->posts.ID) 
            WHERE $wpdb->posts.post_status = \'publish\' 
            AND $wpdb->posts.post_type = \'showroom\' 
            ORDER BY $wpdb->posts.post_title ASC
            ";

// get results from the search query
$searchresults = $wpdb->get_results($querystr, OBJECT);
更新:帖子已经排序了,我正在尝试提取搜索结果的最低和最高元值(price),以便显示价格范围。那么,如何从最终搜索查询中提取最小/最大元值并显示它呢。沿着这条线的东西。

   $query = "
    SELECT DISTINCT {$wpdb->posts}.ID, 
    {$wpdb->posts}.*,
    (SELECT min(cast(meta_value as unsigned)) 
          FROM {$wpdb->postmeta} 
          WHERE meta_key=\'_wp_price\' 
          AND meta_value IS NOT NULL 
          AND meta_value <> 0
          AND {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID
    ) as min_price,
    FROM {$wpdb->posts}
    LEFT JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)
    LEFT JOIN {$wpdb->term_taxonomy} ON({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id)
    LEFT JOIN {$wpdb->terms} ON ({$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id) 
    LEFT JOIN {$wpdb->postmeta} ON ({$wpdb->postmeta}.post_id = {$wpdb->posts}.ID) 
    WHERE {$wpdb->posts}.post_status = \'publish\' 
    AND {$wpdb->posts}.post_type = \'showroom\'"; 

   $the_min = $wpdb->get_var($query); // get the min price of search
     echo \'<span class="pricerange">Price range: \'.ft_currency_conv(of_get_option(\'ft_currency\')).\'\'.$the_min.\' to</span>\'; // display the min price of search above post results.

1 个回复
SO网友:s_ha_dum

你需要subqueries. 这确实是一个SQL问题,但据我所知,如果不循环并运行多个查询,就无法使用本机WordPress函数做到这一点。

$querystr = "
        SELECT DISTINCT {$wpdb->posts}.ID, 
        {$wpdb->posts}.*,
        (SELECT min(cast(meta_value as unsigned)) 
              FROM {$wpdb->postmeta} 
              WHERE meta_key=\'_wp_price\' 
              AND meta_value IS NOT NULL 
              AND meta_value <> 0
              AND {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID
        ) as min_price,
        (SELECT max(cast(meta_value as unsigned)) 
              FROM {$wpdb->postmeta} 
              WHERE meta_key=\'_wp_price\' 
              AND meta_value IS NOT NULL 
              AND meta_value <> 0
              AND {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID
        ) as max_price,
        FROM {$wpdb->posts}
        LEFT JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)
        LEFT JOIN {$wpdb->term_taxonomy} ON({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id)
        LEFT JOIN {$wpdb->terms} ON ({$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id) 
        LEFT JOIN {$wpdb->postmeta} ON ({$wpdb->postmeta}.post_id = {$wpdb->posts}.ID) 
        WHERE {$wpdb->posts}.post_status = \'publish\' 
        AND {$wpdb->posts}.post_type = \'showroom\' 
        ORDER BY {$wpdb->posts}.post_title ASC
        ";
Note: 我的数据库中没有你的数据,所以我无法测试,但我确信它至少非常接近正确。

结束

相关推荐

使用TAX_QUERY时,结果消失(0=1生成)

我正在尝试执行一个动态查询,该查询使用动态传递的分类法和post\\u类型,并相应地显示结果-用户从表单上的3个下拉列表中进行选择,每个下拉列表都有一个值。这是通过URL查询字符串传递的。页面上的查询从URL中提取参数,利用$_GET.当我只拉post\\u类型和其他一些片段时,这段代码工作得很好。global $query $dat_argument = array ( \'post_type\' => $type, \'orderb