我遇到以下查询的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因此,在单独的表towns
和counties
中。如果一个房产没有特别优惠,它仍然应该返回数据。
查询是......
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
)
)
感谢您的帮助。
答案 0 :(得分: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
)
)
)
答案 1 :(得分:0)
缺少一个右括号(可能在最后一行)。
答案 2 :(得分: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
)
)
)