2016-10-07 54 views
1

我需要將這兩個查詢放在一個查詢中的幫助。例如,我想在數據庫中搜索與$ sql的半徑和$ sql2的半徑相匹配的旅程,並獲得與兩者匹配的結果。如何加入這些查詢

$sql = "SELECT adpastrip_id, 
      (3959 * acos(cos(radians($lat)) * cos(radians(lat1)) * cos(radians(long1) - radians($lng)) + sin(radians($lat)) * sin(radians(lat1)))) AS distance 
     FROM addgoodstrip 
     HAVING distance < 10 
     ORDER BY distance 
     LIMIT 0 , 20"; 

$sql2 = "SELECT adpastrip_id, 
      (3959 * acos(cos(radians($lat2)) * cos(radians(lat2)) * cos(radians(long2) - radians($lng2)) + sin(radians($lat2)) * sin(radians(lat2)))) AS distance 
     FROM addgoodstrip 
     HAVING distance < 10 
     ORDER BY distance 
     LIMIT 0 , 20 "; 

回答

0
$sql = "SELECT adpastrip_id, (3959 * acos(cos(radians($lat)) * 
cos(radians(lat1)) * cos(radians(long1) - radians($lng)) + 
sin(radians($lat)) * sin(radians(lat1)))) AS distance, 
(3959 * acos(cos(radians($lat2)) * 
cos(radians(lat2)) * cos(radians(long2) - radians($lng2)) + 
sin(radians($lat2)) * sin(radians(lat2)))) AS distance2 
FROM addgoodstrip HAVING distance < 10 or distance2<10 ORDER BY 
distance ASC LIMIT 0 , 20"; 
0
$sql = "SELECT adpastrip_id, 
     (3959 * acos(cos(radians($lat)) * cos(radians(lat1)) * cos(radians(long1) - radians($lng)) + sin(radians($lat)) * sin(radians(lat1)))) AS distance 
    FROM addgoodstrip 
    HAVING distance < 10 
    ORDER BY distance 
    LIMIT 0 , 20 
    union 
    SELECT adpastrip_id, 
     (3959 * acos(cos(radians($lat2)) * cos(radians(lat2)) * cos(radians(long2) - radians($lng2)) + sin(radians($lat2)) * sin(radians(lat2)))) AS distance 
    FROM addgoodstrip 
    HAVING distance < 10 
    ORDER BY distance 
    LIMIT 0 , 20 "; 

Union不會顯示重複的行,因爲它是相同的,因爲我們對集執行聯盟(非常明確的不同對象的集合),如果你需要重複以及使用union all

+0

聯合查詢不會返回匹配兩個距離準則,它將返回那些符合至少一個,但不一定是另一種。 – Shadow

+0

@Shadow union會合並2個查詢的結果。我希望這是OP所需要的 –

1

你不需要一個JOIN,所有你需要做的是產生在同一個查詢2個計算

$sql = "SELECT adpastrip_id, 
     (3959 * acos(cos(radians($lat)) * cos(radians(lat1)) * cos(radians(long1) - radians($lng)) + sin(radians($lat)) * sin(radians(lat1)))) 
     AS distance1, 
     (3959 * acos(cos(radians($lat2)) * cos(radians(lat2)) * cos(radians(long2) - radians($lng2)) + sin(radians($lat2)) * sin(radians(lat2)))) 
     AS distance2 

    FROM addgoodstrip 
    HAVING distance < 10 
    ORDER BY distance1 
    LIMIT 0 , 20"; 

這將產生一個結果集,像

adpastrip_id distance1 distance2 
1    100   120 
2    90   88 
0

我不認爲你需要一個要麼加入,只包括在他們兩人的where子句中單個查詢和過濾兩種計算得到匹配的結果這兩個條件,並且爲了以最少的距離:

SELECT adpastrip_id, 
    (3959 * acos(cos(radians($lat)) * cos(radians(lat1)) * cos(radians(long1) - radians($lng)) + sin(radians($lat)) * sin(radians(lat1)))) 
    AS distance1, 
    (3959 * acos(cos(radians($lat2)) * cos(radians(lat2)) * cos(radians(long2) - radians($lng2)) + sin(radians($lat2)) * sin(radians(lat2)))) 
    AS distance2 

FROM addgoodstrip 
WHERE (3959 * acos(cos(radians($lat)) * cos(radians(lat1)) * cos(radians(long1) - radians($lng)) + sin(radians($lat)) * sin(radians(lat1)))) <10 AND 
    (3959 * acos(cos(radians($lat2)) * cos(radians(lat2)) * cos(radians(long2) - radians($lng2)) + sin(radians($lat2)) * sin(radians(lat2)))) < 10 

ORDER BY LEAST(distance1,distance2) 
LIMIT 0 , 20";