使用MySQL从产品价格中获取小数

时间:2020-03-12 作者:ilanb

我试图获得如下最低产品价格:

$pts = $wpdb->get_results("SELECT post_title, ID, MIN( CAST(wm.meta_value AS UNSIGNED)) AS price FROM phiz_posts
        INNER JOIN phiz_postmeta wm ON (wm.post_id = phiz_posts.ID)
        INNER JOIN phiz_term_relationships wtr ON (phiz_posts.ID = wtr.object_id)
        INNER JOIN phiz_term_taxonomy wtt ON (wtr.term_taxonomy_id = wtt.term_taxonomy_id)
        INNER JOIN phiz_terms wt ON (wt.term_id = wtt.term_id) AND ((wtt.taxonomy = \'product_visibility\' AND  wt.slug = \'featured\'))
        INNER JOIN phiz_term_relationships rs ON rs.object_id = phiz_posts.ID    
        WHERE post_type = \'product\' 
        AND post_title = \'$title\'
        AND post_status = \'publish\' 
        AND rs.term_taxonomy_id = $product_cat_id
        AND (wm.meta_key = \'_regular_price\'
              OR wm.meta_key = \'_sale_price\'
              OR wm.meta_key = \'_price\')
        ORDER BY price");

        foreach($pts as $row)
        {
            $price = $row->price;
        }
这项工作很好,I\'v是正确的产品,但没有小数。。。这里的结果是9,但实际最低价格是9,60

谢谢

1 个回复
最合适的回答,由SO网友:Mikhail 整理而成

答案就在第一行SELECT post_title, ID, MIN( CAST(wm.meta_value AS UNSIGNED)) AS price - 你投到这里了UNSIGNED 它是整数,这是小数点的一部分。

所以解决方法就是更换UNSIGNED 具有DECIMAL(10,2)FLOAT

结果代码将以浮点数的方式显示

    $pts = $wpdb->get_results("SELECT post_title, ID, MIN( CAST(wm.meta_value AS FLOAT)) AS price FROM phiz_posts
            INNER JOIN phiz_postmeta wm ON (wm.post_id = phiz_posts.ID)
            INNER JOIN phiz_term_relationships wtr ON (phiz_posts.ID = wtr.object_id)
            INNER JOIN phiz_term_taxonomy wtt ON (wtr.term_taxonomy_id = wtt.term_taxonomy_id)
            INNER JOIN phiz_terms wt ON (wt.term_id = wtt.term_id) AND ((wtt.taxonomy = \'product_visibility\' AND  wt.slug = \'featured\'))
            INNER JOIN phiz_term_relationships rs ON rs.object_id = phiz_posts.ID    
            WHERE post_type = \'product\' 
            AND post_title = \'$title\'
            AND post_status = \'publish\' 
            AND rs.term_taxonomy_id = $product_cat_id
            AND (wm.meta_key = \'_regular_price\'
                  OR wm.meta_key = \'_sale_price\'
                  OR wm.meta_key = \'_price\')
            ORDER BY price");

    foreach($pts as $row)
    {
        $price = $row->price;
    }

    $pts = $wpdb->get_results("SELECT post_title, ID, MIN( CAST(wm.meta_value AS DECIMAL(10,2))) AS price FROM phiz_posts
            INNER JOIN phiz_postmeta wm ON (wm.post_id = phiz_posts.ID)
            INNER JOIN phiz_term_relationships wtr ON (phiz_posts.ID = wtr.object_id)
            INNER JOIN phiz_term_taxonomy wtt ON (wtr.term_taxonomy_id = wtt.term_taxonomy_id)
            INNER JOIN phiz_terms wt ON (wt.term_id = wtt.term_id) AND ((wtt.taxonomy = \'product_visibility\' AND  wt.slug = \'featured\'))
            INNER JOIN phiz_term_relationships rs ON rs.object_id = phiz_posts.ID    
            WHERE post_type = \'product\' 
            AND post_title = \'$title\'
            AND post_status = \'publish\' 
            AND rs.term_taxonomy_id = $product_cat_id
            AND (wm.meta_key = \'_regular_price\'
                  OR wm.meta_key = \'_sale_price\'
                  OR wm.meta_key = \'_price\')
            ORDER BY price");

    foreach($pts as $row)
    {
        $price = $row->price;
    }
对于十进制,总数字长度为10位,小数点后可以有2位,因此最好调整这些十进制参数,使其更适合您的数字范围。十进制在精度上胜过浮点数,但您需要注意可能的位数。