Javascript 在SQL查询中获取列名

Javascript 在SQL查询中获取列名,javascript,sql,Javascript,Sql,如何从WHERE语句中的SELECT访问列?我可能缺少引用。对于上下文,这是在一个控制器中,在Strapi CMS中,它运行在node.js服务器上 问题: 在和语句处发生(主要是第一行st\u fromtext行): const rawBuilder=strapi.connections.default.raw( ` 挑选 locations.id作为Location\u id, 位置。标题作为位置\u标题, 位置。纬度作为位置和纬度, 位置。经度作为位置经度, 我是说, 东西,头衔, 事情,

如何从WHERE语句中的SELECT访问列?我可能缺少引用。对于上下文,这是在一个控制器中,在Strapi CMS中,它运行在node.js服务器上

问题: 在和语句处发生(主要是第一行
st\u fromtext
行):

const rawBuilder=strapi.connections.default.raw(
`
挑选
locations.id作为Location\u id,
位置。标题作为位置\u标题,
位置。纬度作为位置和纬度,
位置。经度作为位置经度,
我是说,
东西,头衔,
事情,地点
从地点
右连接事物
ON locations.id=things.Location
WHERE things.Style=`+ctx.query['Style.id']+`
和圆形(st_距离_球面(
st_geomfromtext(CONCAT('点(',位置.经度,',位置.纬度')),
st_geomfromtext(CONCAT(`+ctx.query.Longitude+`+ctx.query.Latitude+`'))

))下面是SQL Server中的一个工作示例,可以帮助您解决此问题

请尝试以下步骤:

  • 从where子句中删除“AND”语句并将其保存在某个位置

  • 添加一些筛选条件,只会为您提供几个已知位置

  • 在每个函数的选择条件中添加新的输出字段,以便了解要比较的内容

    从位置中选择CONCAT('点(',locations.Longitude',,locations.Latitude'))

    从locations中选择st_geomfromtext(CONCAT('点(',locations.Longitude',,locations.Latitude')) 注意:geo函数的输出可能看起来像0xE6100000010C75931804564253C042C042CF66D5E7724340

  • 一旦这些值按照您期望的方式排列好,然后添加一个新版本的where子句以及您所做的调整

  • 检查st_distance_sphere函数的精度。在SQL Server中,该函数默认为米

  • SQL Server中的示例

    CREATE TABLE #locations (id INT, Title VARCHAR(50), Latitude DECIMAL(10,4), Longitude DECIMAL(10,4))
    CREATE TABLE #things (id INT, Title VARCHAR(50), LocationId INT)
    
    INSERT INTO #locations (id, Title, Latitude, Longitude) Values (1,'WH', 38.8977, -77.0365)
    INSERT INTO #locations (id, Title, Latitude, Longitude) Values (2,'CB', 38.8899, -77.0091)
    INSERT INTO #things (id, Title, LocationId) Values (100,'White House',1)
    INSERT INTO #things (id, Title, LocationId) Values (101,'United States Capitol',2)
    
    --My Location at the Washington Monument
    DECLARE @myLat DECIMAL(10,4) = 38.8895;
    DECLARE @myLong DECIMAL(10,4) = -77.0353
    
    SELECT    
        loc.id as Location_ID,
        loc.Title as Location_Title,
        loc.Latitude as Location_Latitude,
        loc.Longitude as Location_Longitude,
        th.id,
        th.Title,
        th.LocationId,
        geometry::STGeomFromText(CONCAT('POINT(',loc.Longitude, ' ', loc.Latitude,')'),4326) as ItemPoint,
        geometry::STGeomFromText(CONCAT('POINT(',@myLat,' ',@myLong,')'),4326) as MyPoint,
        geometry::STGeomFromText(CONCAT('POINT(',loc.Longitude, ' ', loc.Latitude,')'),4326).STDistance(geometry::STGeomFromText(CONCAT('POINT(',@myLat,' ',@myLong,')'),4326))
    FROM #locations loc
    RIGHT JOIN #things th ON loc.id = th.LocationId
    
    DROP TABLE #locations
    DROP TABLE #things
    

    右联接返回常规的内部联接结果。如果要获得真正的右联接结果,请将位置条件从何处移动到ON。若要访问选择列表列,请将选择包装为子查询,然后可以使用外部的选定列。
    AND round(st_distance_sphere(
        st_geomfromtext(CONCAT('POINT(` + ctx.query.Longitude1 + ` ` + ctx.query.Latitude1 + `)')),          
        st_geomfromtext(CONCAT('POINT(` + ctx.query.Longitude2 + ` ` + ctx.query.Latitude2 + `)'))
    )) <= ` + 5000
    
    CREATE TABLE #locations (id INT, Title VARCHAR(50), Latitude DECIMAL(10,4), Longitude DECIMAL(10,4))
    CREATE TABLE #things (id INT, Title VARCHAR(50), LocationId INT)
    
    INSERT INTO #locations (id, Title, Latitude, Longitude) Values (1,'WH', 38.8977, -77.0365)
    INSERT INTO #locations (id, Title, Latitude, Longitude) Values (2,'CB', 38.8899, -77.0091)
    INSERT INTO #things (id, Title, LocationId) Values (100,'White House',1)
    INSERT INTO #things (id, Title, LocationId) Values (101,'United States Capitol',2)
    
    --My Location at the Washington Monument
    DECLARE @myLat DECIMAL(10,4) = 38.8895;
    DECLARE @myLong DECIMAL(10,4) = -77.0353
    
    SELECT    
        loc.id as Location_ID,
        loc.Title as Location_Title,
        loc.Latitude as Location_Latitude,
        loc.Longitude as Location_Longitude,
        th.id,
        th.Title,
        th.LocationId,
        geometry::STGeomFromText(CONCAT('POINT(',loc.Longitude, ' ', loc.Latitude,')'),4326) as ItemPoint,
        geometry::STGeomFromText(CONCAT('POINT(',@myLat,' ',@myLong,')'),4326) as MyPoint,
        geometry::STGeomFromText(CONCAT('POINT(',loc.Longitude, ' ', loc.Latitude,')'),4326).STDistance(geometry::STGeomFromText(CONCAT('POINT(',@myLat,' ',@myLong,')'),4326))
    FROM #locations loc
    RIGHT JOIN #things th ON loc.id = th.LocationId
    
    DROP TABLE #locations
    DROP TABLE #things