简体   繁体   English

have 子句中的未知聚合列

[英]Unknown aggregate column in having clause

im trying to build a location based event search in PHP + MySQL (Using Laravel and its Eloquent ORM)我正在尝试在 PHP + MySQL 中构建基于位置的事件搜索(使用 Laravel 及其 Eloquent ORM)

This is the query I am using:这是我正在使用的查询:

select 
    events.*, 
    ( 3959 * acos( cos( radians(50.5) ) * cos( radians( addresses.latitude ) ) * cos( radians( addresses.longitude ) - radians(9.50) ) + sin( radians(50.5) ) * sin( radians( addresses.latitude ) ) ) ) AS distance

 from 
    `events` inner join `addresses` on `events`.`address_id` = `addresses`.`id`
 having 
    `distance` <= 10 
 order by 
    `id` desc limit 15 offset 0

Im wondering why this error pops up, even though the distance column is in the "select statement".我想知道为什么会弹出这个错误,即使距离列在“选择语句”中。

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'distance' in 'having clause' (SQL: select count(*) as aggregate from `events` inner join `addresses` on `events`.`address_id` = `addresses`.`id` having `distance` <= 10 order by `id` desc)"

This is the PHP Code I use to add the scope to the base query:这是我用来将范围添加到基本查询的 PHP 代码:

    $selectDistance =
        '( 3959 * acos( cos( radians(' . $latitude . ') ) ' .
        '* cos( radians( addresses.latitude ) ) ' .
        '* cos( radians( addresses.longitude ) - radians(' . $longitude . ') ) ' .
        '+ sin( radians(' . $latitude . ') ) ' .
        '* sin( radians( addresses.latitude ) ) ) ) AS distance';

    $query->select(DB::raw('events.*, ' . $selectDistance));
    $query->join('addresses', 'events.address_id', '=', 'addresses.id');
    $query->having('distance', '<=', $km);

Thank you very much :)非常感谢:)

From the error message:从错误消息:

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'distance' in 'having clause' (SQL: select count(*) as aggregate from events inner join addresses on events . address_id = addresses . id having distance <= 10 order by id desc)"未发现柱:“SQLSTATE [42S22] 1054未知列在'距离' '其具有条款'(SQL:SELECT COUNT(*)作为骨料从events内部联接addresseseventsaddress_id = addressesid具有distance <= 10顺序由id desc)"

We can get the following我们可以得到以下

SQL:查询语句:

select count(*) as aggregate 
from `events` inner join `addresses` on `events`.`address_id` = `addresses`.`id` 
having `distance` <= 10 
order by `id` desc

distance is not in the select list. distance不在选择列表中。

The HAVING clause was introduced to SQL because we cannot use aggregate functions with WHERE keyword. HAVING 子句被引入到 SQL 中,因为我们不能使用带有 WHERE 关键字的聚合函数。 According to your query there is no aggregate function within the SELECT statement or the HAVING clause.根据您的查询,SELECT 语句或 HAVING 子句中没有聚合函数。 Therefore try to change因此尝试改变

having 
    `distance` <= 10 

to

where
    `distance` <= 10 

The HAVING clause should be used in below format. HAVING 子句应按以下格式使用。

HAVING aggregate_function(column_name) operator value

Where operator can be =, >, < , <=, ect....其中运算符可以是 =、>、<、<=、等等....

Furthermore your PHP code also need to be changed accordingly.此外,您的 PHP 代码也需要相应地更改。

Below SQL tutorial link might help you to better understand the use of HAVING clause in SQL.下面的 SQL 教程链接可能会帮助您更好地理解 SQL 中 HAVING 子句的使用。

http://www.w3schools.com/sql/sql_having.asp http://www.w3schools.com/sql/sql_have.asp

You can try something like that你可以试试这样的

$selectDistance =
    '( 3959 * acos( cos( radians(' . $latitude . ') ) ' .
    '* cos( radians( addresses.latitude ) ) ' .
    '* cos( radians( addresses.longitude ) - radians(' . $longitude . ') ) ' .
    '+ sin( radians(' . $latitude . ') ) ' .
    '* sin( radians( addresses.latitude ) ) ) )'; 

$query->join('addresses', 'events.address_id', '=', 'addresses.id')
      ->select(DB::raw('events.*))
      ->selectRaw("{$selectDistance} AS distance")
      ->whereRaw("{$selectDistance} < ?", 10)
      ->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM