如何加入这些查询

时间:2016-10-07 12:37:20

标签: mysql

我需要帮助将这两个查询放入一个查询中。例如,我想在数据库中搜索与$ 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 ";

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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";

答案 2 :(得分: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(一组非常明确的不同对象)相同,如果你需要重复行,那么使用union all

答案 3 :(得分: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";