如何正确子查询 - 或者这是怎么回事?

时间:2014-11-30 03:58:09

标签: php mysql

我正在尝试获取大量数据库条目,其中包括必须从另一个表中获取的地理数据。当我尝试对它们运行自定义距离函数时会导致错误,我试图将其作为子查询包含但是它似乎也不起作用(函数确实有效)。

$bsns = $this->db->select('businesses.name, businesses.about')
                            ->select('postcodes.postcode, postcodes.lat AS lat, postcodes.long AS long')
                            ->select('industries.name')
                            ->select('get_distance_in_miles_between_geo_locations('.$geo['lat'].', '.$geo['long'].', lat, long) AS distance', null, false)
                            ->from('businesses')
                            ->join('postcodes', 'postcodes.postcode = businesses.postcode', 'left')
                            ->join('industries', 'industries.IN_ID = businesses.industry', 'left')
                            ->order_by('distance', 'ASC')
                            ->get(); 

这是我目前正在使用的代码,错误:

Error Number: 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 'long, `industries`.`name`, get_distance_in_miles_between_geo_locations(50.344178' at line 1

新错误:

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 'businesses`) LEFT JOIN `postcodes` ON `postcodes`.`postcode` = `businesses`.`pos' at line 1

SELECT `businesses`.`name`, `businesses`.`about`, `postcodes`.`postcode`, `postcodes`.`lat`, `postcodes`.`long`, `industries`.`name`, get_distance(50.344178, `-4`.`757147`, `lat`, `long`)` AS distance FROM (`businesses`) LEFT JOIN `postcodes` ON `postcodes`.`postcode` = `businesses`.`postcode` LEFT JOIN `industries` ON `industries`.`IN_ID` = `businesses`.`industry` ORDER BY `distance` ASC

1 个答案:

答案 0 :(得分:0)

你正在混合单引号,MySQL解析器不喜欢它。 它认为你完成了你的陈述。

select('get_distance_in_miles_between_geo_locations('.$geo['lat'].', '.$geo['long'].', lat, long) AS distance', null, false)

应该是:

select('get_distance_in_miles_between_geo_locations('.$geo["lat"].', '.$geo["long"].', lat, long) AS distance', null, false)