Как добавить предложение where в псевдоним

0

мой запрос

select 'id', 'name', 'bitrixid', 'pec_public_id', 'name' as 'bitrixname', 'address', 'yandex_map', 'office', 'date_from', 'date_to', 'days', 'cities',
  ( 6371 * acos( cos( radians(42.057669) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(48.288776) ) + sin( radians(42.057669) ) * sin( radians( lat ) ) ) )
    AS distance
from 'a_delivery_pec_cities'
where 'lat' is not null and 'lon' is not null
      and name like '%Derbent%'
      and 'active' = 1
order by 'distance' asc
limit 1
offset 0

я должен добавить предложение, что расстояние с алиасом <100.

если я пытаюсь использовать такие цитаты или без кавычек

и distance <100

я получил ошибку "Неизвестный столбец" расстояние "в 'where clause'"

Теги:

2 ответа

0

Вы можете использовать HAVING в конце инструкции

select 'id', 'name', 'bitrixid', 'pec_public_id', 'name' as 'bitrixname', 'address', 'yandex_map', 'office', 'date_from', 'date_to', 'days', 'cities',
  ( 6371 * acos( cos( radians(42.057669) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(48.288776) ) + sin( radians(42.057669) ) * sin( radians( lat ) ) ) )
    AS distance
from 'a_delivery_pec_cities'
where 'lat' is not null and 'lon' is not null
      and name like '%Derbent%'
      and 'active' = 1
having distance < 100
order by 'distance' asc
  • 1
    В вашем коде есть ошибка синтаксиса, предложение HAVING всегда предшествует предложению ORDER BY .
  • 0
    спасибо, cdaiga
0

Вы не можете использовать псевдоним в предложении where в MySQL.

select 'id', 'name', 'bitrixid', 'pec_public_id', 'name' as 'bitrixname', 'address', 'yandex_map', 'office', 'date_from', 'date_to', 'days', 'cities',
  ( 6371 * acos( cos( radians(42.057669) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(48.288776) ) + sin( radians(42.057669) ) * sin( radians( lat ) ) ) )
    AS distance
from 'a_delivery_pec_cities'
where 'lat' is not null and 'lon' is not null
      and name like '%Derbent%'
      and 'active' = 1
      and ( 6371 * acos( cos( radians(42.057669) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(48.288776) ) + sin( radians(42.057669) ) * sin( radians( lat ) ) ) )<100
order by 'distance' asc
limit 1
offset 0;
  • 1
    извините, но ORDER BY позволяет использовать ALIAS .
  • 0
    Это если псевдоним определен в подзапросе.
Показать ещё 3 комментария

Ещё вопросы

Сообщество Overcoder
Наверх
Меню