在我的项目中,我有来自mysql的wordpress自定义字段数据:
---------------------------------------------------
id | post_id | meta_key | meta_value
---------------------------------------------------
1 | 200 | age_min | 5
2 | 200 | age_max | 8
3 | 399 | ... | ...
我的表结构
id => aut_increment
meta_key => varchar(255)
meta_value => longtext
从我的sql脚本中我必须找到'age_min和age_max'和孩子数之间的孩子:
SELECT DISTINCT p.ID
, a.meta_value as nbrmin, b.meta_value as nbr_enfants_min , c.meta_value as age_min, d.meta_value as age_max
FROM `6288gjvs_posts` as p
LEFT JOIN 6288gjvs_postmeta as a ON a.post_id = p.ID
LEFT JOIN 6288gjvs_postmeta as b ON b.post_id = p.ID
LEFT JOIN 6288gjvs_postmeta as c ON c.post_id = p.ID
LEFT JOIN 6288gjvs_postmeta as d ON d.post_id = p.ID
WHERE p.post_type = 'product' AND post_status = 'publish'
AND a.meta_key = 'childs_min_number'
AND a.meta_value <= 2
AND b.meta_key = 'childs_max_number'
AND b.meta_value >= 2
AND c.meta_key = 'age_min'
AND c.meta_value = 1
AND d.meta_key = 'age_max'
AND d.meta_value >= 10
当我启动此查询时,这不能正常工作,因为meta_value类型是一个longtext而我的php变量($ this-&gt; age_min,$ this-&gt; age_max)是整数,所以我无法比较那两种不同的类型?
我试图像这样转换,没有运气:
... CONVERT(a.meta_value, DECIMAL) ...
如本网站所述:Mysql conversion functions
有人可以帮我吗?
谢谢。
答案 0 :(得分:1)
您不需要转换类型
我认为错误是符合的
AND a.meta_value <= {$this->age_min}
不应该是AND a.meta_value >= {$this->age_min}
?
答案 1 :(得分:1)
请原谅我,如果我错了,但不应该AND b.meta_value >= {$this->age_max}
?