douzhigan1687 2014-11-30 03:58 采纳率: 0%
浏览 32
已采纳

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

I'm trying to fetch a load of database entries which include geo data that has to be fetched from another table. When i try to run a custom distance function on them it causes an error, I have tried to include it as a sub query but that does not seem to work either (The function does work).

$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(); 

This is the code i'm using at the moment, The error:

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

New error:

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条回答 默认 最新

  • doudi8298 2014-11-30 04:03
    关注

    You're mixing single quotes, the MySQL parser does not like it. It thinks you're done with your statement.

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

    should be :

    select('get_distance_in_miles_between_geo_locations('.$geo["lat"].', '.$geo["long"].', lat, long) AS distance', null, false)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵