开发者

Using MySQL Cast to find Wordpress posts with a custom field content equivalent to Price

开发者 https://www.devze.com 2023-01-21 02:03 出处:网络
I am using 开发者_运维知识库custom fields in Wordpress to contain an \'RRP\' currency value.This field is text based, but I want to be able to run queries where I can bring out posts which have an RRP

I am using 开发者_运维知识库custom fields in Wordpress to contain an 'RRP' currency value. This field is text based, but I want to be able to run queries where I can bring out posts which have an RRP in a specific range.

I've been looking into the MySQL CAST function and this seems to be the right thing, but I can't seem to get it working. Everything I use seems to just become 0.00. Can anyone point out where I am going wrong?

SELECT wpostmeta.meta_value, CAST( 'wpostmeta.meta_value' AS DECIMAL( 10, 2 ) ) , wposts . *
FROM tc_posts wposts, tc_postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'rrp'
AND CAST( 'wpostmeta.meta_value' AS DECIMAL( 10, 2 ) ) < 9.99
ORDER BY wposts.post_date DESC


I think I see your problem here. 'wpostmeta.meta_value' will give you 0 when casted, you should get rid of those quotes, since it's the value you want to cast from, not the name of the field:

SELECT wpostmeta.meta_value, CAST(wpostmeta.meta_value AS DECIMAL(10, 2)), wposts.*
FROM tc_posts wposts, tc_postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'rrp'
AND CAST(wpostmeta.meta_value AS DECIMAL(10, 2)) < 9.99
ORDER BY wposts.post_date DESC

Cheers.

0

精彩评论

暂无评论...
验证码 换一张
取 消