其他Where Fields

时间:2015-06-08 06:53:43

标签: php mysql sql wordpress

我正在尝试向此添加另一个WHERE查询,但根本没有任何乐趣。

我有一个包含三个字段的表单:price,min_price和max_price。

我添加了这一行:

WHERE 'price' BETWEEN 'property_min_price' AND 'property_max_price'

这对我来说似乎合乎逻辑,但它不起作用。有小费吗?感谢。

/**
* Set the Query
*/
private function setQuery()
{
    $sql = "
        SELECT 
        p.post_title AS title,
        p.ID AS id," .
        $this->sqlFieldVars() . "
        lat.meta_value AS latitude,
        lng.meta_value AS longitude,
        ( " . $this->query_data['diameter'] . " * acos( cos( radians(@origlat) ) * cos( radians( lat.meta_value ) ) 
        * cos( radians( lng.meta_value ) - radians(@origlng) ) + sin( radians(@origlat) ) * sin(radians(lat.meta_value)) ) )
        AS distance
        FROM " . $this->query_data['post_table'] . " AS p
        LEFT JOIN " . $this->query_data['meta_table'] . " AS lat
        ON p.ID = lat.post_id AND lat.meta_key = '" . $this->query_data['lat_field'] . "'
        LEFT JOIN " . $this->query_data['meta_table'] . " AS lng
        ON p.ID = lng.post_id AND lng.meta_key = '" . $this->query_data['lng_field'] . "'" . 
        $this->sqlFieldJoins() . "      
        WHERE lat.meta_value
            BETWEEN @origlat - (@distance / @dist_unit)
            AND @origlat + (@distance / @dist_unit)
        AND lng.meta_value
            BETWEEN @origlng - (@distance / (@dist_unit * cos(radians(@origlat))))
            AND @origlng + (@distance / (@dist_unit * cos(radians(@origlat))))
        AND `post_type` = '" . $this->query_data['post_type'] . "'
        AND `post_status` = 'publish'
        WHERE 'price' BETWEEN 'property_min_price' AND 'property_max_price'
        HAVING distance < @distance
        ORDER BY distance\n" . 
        $this->sqlLimit() . ";";
    $this->sql = $sql;
}

2 个答案:

答案 0 :(得分:1)

通常,在SQL字符串的末尾只有一个WHERE语句。我注意到你有几个连接,后跟一个WHERE语句,紧接着是第二个WHERE语句。

您是否尝试过将第二个WHERE更改为AND?

        和post_status =&#39;发布&#39;
         AND &#39;价格&#39; BETWEEN&#39; property_min_price&#39; AND&#39; property_max_price&#39;

答案 1 :(得分:1)

您只能有一个WHERE子句。要向现有WHERE添加其他条件,请使用AND

... AND `post_status` = 'publish'
    AND `price` BETWEEN 'property_min_price' AND 'property_max_price' ...

另外,我猜,后面的标记 - 不是单引号 - 应该用于列名。 (但我不是MySQL人,所以我不确定......)