我在Wordpress中使用自定义字段来包含“ RRP”货币值。该字段是基于文本的,但是我希望能够运行查询,在其中我可以带出R​​RP在特定范围内的帖子。

我一直在研究MySQL CAST函数,这似乎是正确的事情,但是我似乎无法使其正常运行。我使用的所有内容似乎都变为0.00。谁能指出我要去哪里了?

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

最佳答案

我想我在这里看到你的问题。 'wpostmeta.meta_value'在强制转换时将为您提供0,您应该删除那些引号,因为它是您要强制转换的值,而不是字段的名称:

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


干杯。

10-08 09:05