答案就在第一行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位,因此最好调整这些十进制参数,使其更适合您的数字范围。十进制在精度上胜过浮点数,但您需要注意可能的位数。