0

我对以下查询有 sql 问题,它返回错误...

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''

我不明白为什么这会引发错误。当我删除连接的部分时,它运行正常。

此查询的工作是根据给定的位置(纬度/经度)返回一个属性列表,其中包含来自单独表的任何匹配数据以及相应offers地存储在单独表中的城镇和县的 url slug 。如果一个属性没有特价,它仍然应该在数据中返回。townscounties

查询是...

    SELECT COUNT(agencyid) AS `count_offers`, prop.*, offers.*, twn.slug AS `slug_town`, cnty.slug AS `slug_county`, ROUND(((3959 * acos(cos(radians(50.1854670)) * cos(radians(prop.latitude)) * cos(radians(prop.longitude) - radians(-5.4209100)) + sin(radians(50.1854670)) * sin( radians(prop.latitude)))) * 2),0)/2 AS `distance` 
FROM `properties` AS `prop`

JOIN `towns` AS `twn` ON twn.name=prop.town
JOIN `counties` AS `cnty` ON cnty.name=twn.county
LEFT JOIN `offers` ON offers.agencyid = prop.mID
AND 
offers.dt_expire>1377985886 
AND 
(prop.sleeps >= offers.sleeps_min AND prop.sleeps <= offers.sleeps_max)

AND 
(
  prop.slug_code = offers.the_property 
  OR 
  (
    (offers.the_property='' OR offers.the_property=NULL) 
    AND 
    (
      (
        prop.county = offers.the_county 
        AND
        prop.town=offers.the_town
      ) 
      OR 
      (
        prop.county = offers.the_county
        AND
        twn.name=offers.the_town
        AND
        prop.place = offers.the_place
      ) 
      OR
      prop.county = offers.the_county 
      OR
      prop.region = offers.the_region 
      OR
      prop.country = offers.the_country
    )
  )

感谢您对此的帮助。

4

3 回答 3

1

在 MySQL 中,如果你需要比较,NULL那么语法是Colunname IS NULL

SELECT COUNT(agencyid) AS `count_offers`, prop.*, offers.*, twn.slug AS `slug_town`, cnty.slug AS `slug_county`, ROUND(((3959 * acos(cos(radians(50.1854670)) * cos(radians(prop.latitude)) * cos(radians(prop.longitude) - radians(-5.4209100)) + sin(radians(50.1854670)) * sin( radians(prop.latitude)))) * 2),0)/2 AS `distance` 
FROM `properties` AS `prop`

JOIN `towns` AS `twn` ON twn.name=prop.town
JOIN `counties` AS `cnty` ON cnty.name=twn.county
LEFT JOIN `offers` ON offers.agencyid = prop.mID
AND 
offers.dt_expire>1377985886 
AND 
(prop.sleeps >= offers.sleeps_min AND prop.sleeps <= offers.sleeps_max)

AND 
(
  prop.slug_code = offers.the_property 
  OR 
  (
    (offers.the_property='' OR offers.the_property IS NULL) 
    AND 
    (
      (
        prop.county = offers.the_county 
        AND
        prop.town=offers.the_town
      ) 
      OR 
      (
        prop.county = offers.the_county
        AND
        twn.name=offers.the_town
        AND
        prop.place = offers.the_place
      ) 
      OR
      prop.county = offers.the_county 
      OR
      prop.region = offers.the_region 
      OR
      prop.country = offers.the_country
    )
  )
)
于 2013-09-02T11:56:43.070 回答
0

缺少一个右括号(可能在最后一行)。

于 2013-09-02T11:58:03.933 回答
0

在没有一些示例数据或表结构可玩的情况下回答这样的问题是非常棘手的,但我建议这就是你所追求的。

如果我没记错的话,MySql 不会将 '' 评估为 NULL,但如果我错了,请从我上面的评论中获取条件。

SELECT 
  COUNT(agencyid) AS `count_offers`, 
  prop.*, 
  offers.*, 
  twn.slug AS `slug_town`, 
  cnty.slug AS `slug_county`, 
  ROUND(((3959 * acos(cos(radians(50.1854670)) * cos(radians(prop.latitude)) * cos(radians(prop.longitude) - radians(-5.4209100)) + sin(radians(50.1854670)) * sin( radians(prop.latitude)))) * 2),0)/2 AS `distance` 
FROM 
  `properties` AS `prop`
  JOIN `towns` AS `twn` ON 
    twn.name=prop.town
  JOIN `counties` AS `cnty` ON 
    cnty.name=twn.county
  LEFT JOIN `offers` ON 
    offers.agencyid = prop.mID AND 
    offers.dt_expire>1377985886 AND 
    prop.sleeps >= offers.sleeps_min AND 
    prop.sleeps <= offers.sleeps_max AND
    (
      prop.slug_code = offers.the_property OR 
      (
        (IfNull(offers.the_property, '') = '') AND 
        (
          (
            prop.county = offers.the_county AND
            prop.town=offers.the_town
          ) 
          OR 
          (
            prop.county = offers.the_county AND
            twn.name=offers.the_town AND
            prop.place = offers.the_place
          ) 
          OR
          prop.county = offers.the_county OR
          prop.region = offers.the_region OR
          prop.country = offers.the_country
        )
      )
    )
于 2013-09-02T13:54:40.097 回答