gpt4 book ai didi

php - 计算数组中值的数量

转载 作者:行者123 更新时间:2023-12-01 00:54:16 24 4
gpt4 key购买 nike

我目前正在尝试计算特定价格范围内的特性数量,而我正在运行的查询仅返回值 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);

非常感谢。

最佳答案

使用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 计算全部:

$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;

关于php - 计算数组中值的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12555547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com