我目前正在尝试计算在特定价格范围内有多少属性,而我正在运行的查询只返回值1.显然我做错了 - 这并没有帮助不是这样MySQL / PhP精明.. 有人会介意伸手吗?
查询如下:
$apart = $wpdb->get_var("SELECT m1.post_id
FROM hj_postmeta m1
INNER JOIN hj_postmeta m2 ON m1.post_id = m2.post_id
WHERE m1.meta_key = 'price'
AND m1.meta_value
BETWEEN 50000 AND 74999 AND m2.meta_key = 'property_type'
AND m2.meta_value = 'apartment'");
$apartprice = count($apart);
非常感谢提前。
答案 0 :(得分:4)
将get_results
用作get_var
function returns a single variable from the database:
$apart = $wpdb->get_results("SELECT m1.post_id
FROM hj_postmeta m1
INNER JOIN hj_postmeta m2 ON m1.post_id = m2.post_id
WHERE m1.meta_key = 'price'
AND m1.meta_value
BETWEEN 50000 AND 74999 AND m2.meta_key = 'property_type'
AND m2.meta_value = 'apartment'");
$apartprice = count($apart);
所有get_var
的或COUNT:
$apart = $wpdb->get_var("SELECT COUNT(m1.post_id)
FROM hj_postmeta m1
INNER JOIN hj_postmeta m2 ON m1.post_id = m2.post_id
WHERE m1.meta_key = 'price'
AND m1.meta_value
BETWEEN 50000 AND 74999 AND m2.meta_key = 'property_type'
AND m2.meta_value = 'apartment'");
$apartprice = $apart;
答案 1 :(得分:1)
只要您对行数感兴趣:
$apart = $wpdb->get_var("....");
$apartprice = $wpdb->num_rows;
但是我想Mihai Iorga's answer提出两个非常公平的点,以防你需要获取所有数据或者如果你实际上没有实际处理下一个结果集,那么数据库中的数据(如上面由juergen d评论的那样)那个变量。