我寫了一個複雜的mysql查詢,它不返回所有請求的數據。看起來像INNER JOIN工作不正常。 這個查詢確實返回了正確的結果集,我只是缺少了連接表中的列。Mysql SELECT查詢HAVING結合INNER JOIN WHERE不顯示所有請求的數據
我的查詢:
SELECT Table1.Col1, Col2, Col3, Col4, Col5, Col6, (6371 * acos(cos(radians(?))
* cos(radians(lat)) * cos(radians(lng) - radians(?)) + sin(radians(?))
* sin(radians(lat)))) AS distance
FROM Table1
INNER JOIN Table2
ON Table1.Col1 = Table2.Col1
WHERE TABLE2.Col2 =?
AND Table2.Col3 =?
HAVING distance < ?
ORDER BY distance
我的結果只包含來自Table1
值,爲什麼不INNER JOIN的工作?
編輯: 我改變了我的查詢來此基礎上P.Jairaj的答案,這似乎爲我工作:
SELECT T1.Col1, T1.Col2, T1.Col3, T1.Col4, T1.Col5, T1.Col6, T2.Col1,
(6371 * ACOS(COS(RADIANS('?')) *
COS(RADIANS(lat)) * COS(RADIANS(lng) - RADIANS('?')) +
SIN(RADIANS('?')) * SIN(RADIANS(lat)))) AS distance
FROM T1, T2
WHERE T1.Col1 = T2.Col1
AND T2.Col2 = '?'
AND T2.Col3 = '?'
HAVING distance < '?'
ORDER BY distance
嘗試'左外join' – RamRaider
您需要在[接受的答案]的習慣得到( http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)幫助你解決你的問題。 –
我得到的答案讓我更接近解決方案,但並非100%正確。在我看來,如果我接受這些作爲答案,它會混淆未來的訪問者... – user3055582