如何处理我的SQL中的歧义字段

时间:2012-06-04 18:05:02

标签: mysql sql

SELECT TB.ID, Latitude, Longitude, 111151.29341326 * SQRT( POW( -6 - `Latitude` , 2 ) + POW( 106 - `Longitude` , 2 ) * COS( -6 * 0.017453292519943 ) * COS( `Latitude` * 0.017453292519943 ) ) AS Distance
FROM `tablebusiness` AS TB
JOIN `tablecity` AS TC ON TB.City = TC.City
JOIN `businessestag` AS BC ON BC.BusinessID = TB.ID
JOIN `businessesdistricts` AS BD ON BD.BusinessID = TB.ID
JOIN `tabledistrict` AS TD ON TD.ID = BD.District
WHERE (
`Title` LIKE '%restaurant%'
OR `Street` LIKE '%restaurant%'
OR TB.City LIKE '%restaurant%'
OR BC.Tag LIKE '%restaurant%'
OR TD.District LIKE '%restaurant%'
)
AND (
- 6.0917668133836 < `Latitude`
AND `Latitude` < - 5.9082331866164
AND 105.90823318662 < `Longitude`
AND `Longitude` < 106.09176681338
)
ORDER BY Distance
LIMIT 0, 100

这个mysql经历了

然后我想看看建筑也是如此。

所以我做了,就像有人建议做的那样

SELECT
  TB.ID,
  Latitude,
  Longitude,
  111151.29341326 * SQRT(POW(-6 - `TB.Latitude`, 2) + POW(106 - `TB.Longitude`, 2) * COS(-6 * 0.017453292519943) * COS(`TB.Latitude` * 0.017453292519943)) AS Distance
FROM
  `tablebusiness` AS TB
  JOIN `tablecity` AS TC
    ON TB.City = TC.City
  JOIN `businessestag` AS BC
    ON BC.BusinessID = TB.ID
  JOIN `businessesdistricts` AS BD
    ON BD.BusinessID = TB.ID
  JOIN `tabledistrict` AS TD
    ON TD.ID = BD.District
  LEFT JOIN `tablebusiness` TBuilding
    ON TBuilding.ID = TB.Building
WHERE
  (`Title` LIKE '%restaurant%' OR `Street` LIKE '%restaurant%' OR TB.City LIKE '%restaurant%' 
    OR BC.Tag LIKE '%restaurant%' OR TD.District LIKE '%restaurant%' OR TBuilding.Title LIKE '%restaurant%')
  AND (-6.0917668133836 < `TB.Latitude` AND `TB.Latitude` < -5.9082331866164 
    AND 105.90823318662 < `TB.Longitude` AND `TB.Longitude` < 106.09176681338) 
ORDER BY
  Distance
LIMIT
  0, 100

然后我收到一条消息,称纬度字段不明确。

我该怎么办?

我做了明显的添加TB。在纬度之前

explain SELECT
  TB.ID,
  TB.Latitude,
  TB.Longitude,
  111151.29341326 * SQRT(POW(-6 - `TB.Latitude`, 2) + POW(106 - `TB.Longitude`, 2) * COS(-6 * 0.017453292519943) * COS(`TB.Latitude` * 0.017453292519943)) AS Distance
FROM
  `tablebusiness` AS TB
  JOIN `tablecity` AS TC
    ON TB.City = TC.City
  JOIN `businessestag` AS BC
    ON BC.BusinessID = TB.ID
  JOIN `businessesdistricts` AS BD
    ON BD.BusinessID = TB.ID
  JOIN `tabledistrict` AS TD
    ON TD.ID = BD.District
  LEFT JOIN `tablebusiness` TBuilding
    ON TBuilding.ID = TB.Building
WHERE
  (`Title` LIKE '%restaurant%' OR `Street` LIKE '%restaurant%' OR TB.City LIKE '%restaurant%' 
    OR BC.Tag LIKE '%restaurant%' OR TD.District LIKE '%restaurant%' OR TBuilding.Title LIKE '%restaurant%')
  AND (-6.0917668133836 < `TB.Latitude` AND `TB.Latitude` < -5.9082331866164 
    AND 105.90823318662 < `TB.Longitude` AND `TB.Longitude` < 106.09176681338) 
ORDER BY
  Distance
LIMIT
  0, 100

现在错误是#1054 - '字段列表'中的未知列'TB.Latitude'

4 个答案:

答案 0 :(得分:3)

我想您需要在Latitude别名前加上TB

SELECT
  TB.ID,
  TB.Latitude,
...

可能会发生Longitude也不明确,所以我建议在前面加上适当的别名。

<强> UPD:

我还建议您删除围绕TB.LatitudeTB.Latitude的引号,因为这些引起“未知列”错误。

答案 1 :(得分:2)

放置表格的别名,如TB.Latitude

答案 2 :(得分:0)

由于在第二个查询中您加入tablebusiness两次,因此会产生歧义。

因此,为了消除歧义,您还必须包含表格的别名Latitude and Longitude

正确的查询是 -

SELECT TB.ID,
       TB.Latitude,
       TB.Longitude,
<everything else remains the same.>

答案 3 :(得分:0)

正确答案是Li0LiQ建议的答案

所以我不选择这个作为答案。

只是为了记录,最后的案例是,它的工作原理。再次。谢谢Li0LiQ。您的答案是选定的答案。

explain SELECT
  TB.ID,
  TB.Latitude,
  TB.Longitude,
  111151.29341326 * SQRT(POW(-6 - TB.Latitude, 2) + POW(106 - TB.Longitude, 2) * COS(-6 * 0.017453292519943) * COS(TB.Latitude * 0.017453292519943)) AS Distance
FROM
  `tablebusiness` AS TB
  JOIN `tablecity` AS TC
    ON TB.City = TC.City
  JOIN `businessestag` AS BC
    ON BC.BusinessID = TB.ID
  JOIN `businessesdistricts` AS BD
    ON BD.BusinessID = TB.ID
  JOIN `tabledistrict` AS TD
    ON TD.ID = BD.District
  LEFT JOIN `tablebusiness` TBuilding
    ON TBuilding.ID = TB.Building
WHERE
  (TB.Title LIKE '%restaurant%' OR TB.Street LIKE '%restaurant%' OR TB.City LIKE '%restaurant%' 
    OR BC.Tag LIKE '%restaurant%' OR TD.District LIKE '%restaurant%' OR TBuilding.Title LIKE '%restaurant%')
  AND (-6.0917668133836 < TB.Latitude AND TB.Latitude < -5.9082331866164 
    AND 105.90823318662 < TB.Longitude AND TB.Longitude < 106.09176681338) 
ORDER BY
  Distance
LIMIT
  0, 100