【问题标题】:How to use having with or condition in mysql?如何在mysql中使用with或condition?
【发布时间】:2016-09-08 11:52:36
【问题描述】:

我有两个具有相同表名的类似查询,它们单独工作正常。但我想将它们与条件 ((city_id = 1 and country_id = 1) 或距离

合并

第一次查询

select id , lat , long , city_id , country_id 
from element_table
where city_id = 1 and country_id = 1 and id < 20 

第二次查询 在这里,我使用经纬度搜索

 select id , lat , long , city_id , country_id ,
 my_distance_calculationformula_with_lat_long as distance  
 from element_table where id < 20 having distance = "20"

【问题讨论】:

标签: mysql sql


【解决方案1】:

你可以使用 where 和 have

select id , lat , long , city_id , country_id ,
my_distance_calculationformula_with_lat_long as distance  
from element_table
where city_id = 1 and country_id = 1 and id < 20 
having distance = 20

在你的情况下(你的评论) 那么你不应该使用别名而只执行 where

select id , lat , long , city_id , country_id ,
my_distance_calculationformula_with_lat_long as distance  
from element_table
where city_id = 1 and country_id = 1 and id < 20 
or   my_distance_calculationformula_with_lat_long < 20

(如果你的 my_distance_calculationformula_with_lat_long 是一个长的 sql 代码。你应该重复代码不使用别名 ..column alias in not allowed in where)

【讨论】:

  • 我想做这样的条件((city_id = 1 and country_id = 1)或距离
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-03
  • 2023-02-06
  • 2019-12-25
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
相关资源
最近更新 更多