You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现基于单字段多条件的SQL Inner Join关联查询

解决方法:关联两次地理围栏表获取起点终点信息

你需要对geofence表执行两次内连接,分别匹配路由的起点ID和终点ID,通过表别名区分两次查询的结果,最终就能同时获取起点和终点的名称、标签信息。对应的SQL语句如下:

SELECT
  gr.origin_id AS `origin id`,
  geo_origin.name AS `origin name`,
  geo_origin.tags AS `origin tags`,
  gr.destination_id AS `destination id`,
  geo_dest.name AS `destination name`,
  geo_dest.tags AS `destination tags`,
  gr.price
FROM geofence_rates gr
INNER JOIN geofence geo_origin ON gr.origin_id = geo_origin.id
INNER JOIN geofence geo_dest ON gr.destination_id = geo_dest.id;

关键说明:

  • 给geofence_rates表设置别名gr,简化SQL书写
  • 第一次连接geofence表并设置别名geo_origin,用于获取起点的名称和标签
  • 第二次连接geofence表并设置别名geo_dest,用于获取终点的名称和标签
  • 通过AS关键字给查询结果的列设置别名,完全匹配你期望的输出格式

内容的提问来源于stack exchange,提问作者geyser

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 16:10:30