MySQL选择内连接(仅限唯一)

时间:2017-03-25 16:11:10

标签: php mysql pdo

在我的应用中,我有分配到特定课程的车辆。在下面的查询中,我试图选择其中至少有一辆车在某个区域内的所有类。

下面的代码工作正常,但是如果类中有多个适合location参数的工具,则会在结果中返回该类的多个实例。

如何构造此查询,以便只返回每个类的一个实例,无论该类内部有多少车辆符合参数?

$get_veh = $pdo->prepare("SELECT * FROM tbl_car_class_category JOIN tbl_vehicles ON tbl_vehicles.veh_class_id = tbl_car_class_category.id_class_cat WHERE tbl_vehicles.veh_advanceLng between (:loc_long-:radius/cos(radians(:loc_lat))*69) and (:loc_long+:radius/cos(radians(:loc_lat))*69) and veh_advanceLat between (:loc_lat-(:radius/69)) and (:loc_lat+(:radius/69)) AND veh_status=:veh_status");

$get_veh->bindparam(":veh_status", $veh_status);
$get_veh->bindparam(":loc_lat", $loc_lat);
$get_veh->bindparam(":loc_long", $loc_long);
$get_veh->bindparam(":radius", $radius);

$get_veh->execute();

3 个答案:

答案 0 :(得分:2)

如果意图仅返回类别表中的行,我们可以将JOIN替换为EXISTS (correlated subquery)

例如:

 SELECT c.*
   FROM tbl_car_class_category c
  WHERE EXISTS
      ( SELECT 1 
          FROM tbl_vehicles v 
         WHERE v.veh_class_id = c.id_class_cat
           AND v.veh_advanceLng 
               BETWEEN (:loc_long-:radius/cos(radians(:loc_lat))*69)
                   AND (:loc_long+:radius/cos(radians(:loc_lat))*69)
           AND v.veh_advanceLat
               BETWEEN (:loc_lat-(:radius/69)) 
                   AND (:loc_lat+(:radius/69))
           AND v.veh_status=:veh_status
      )

如果我们还需要从车辆表中返回一行,我们可以在内联视图和JOIN中使用聚合。

 SELECT c.*
      , g.*
   FROM tbl_car_class_category c
   JOIN ( SELECT v.veh_class_id
               , MIN(v.id)    AS   min_id   -- PK or unique identifier    
            FROM tbl_vehicles v 
            AND v.veh_advanceLng 
                BETWEEN (:loc_long-:radius/cos(radians(:loc_lat))*69)
                    AND (:loc_long+:radius/cos(radians(:loc_lat))*69)
            AND v.veh_advanceLat
                BETWEEN (:loc_lat-(:radius/69)) 
                    AND (:loc_lat+(:radius/69))
            AND v.veh_status=:veh_status
          GROUP BY v.veh_class_id 
        ) m
     ON m.veh_class_id = c.id_class_cat
   JOIN tbl_vehicles g
     ON g.id           = m.min_id
    AND g.veh_class_id = m.veh_class_id
  ORDER BY ...

(注意:这假定id_class_cat中的tbl_car_class_category是唯一的

答案 1 :(得分:0)

你试过LIMIT吗?

$get_veh = $pdo->prepare("SELECT * FROM tbl_car_class_category JOIN tbl_vehicles ON tbl_vehicles.veh_class_id = tbl_car_class_category.id_class_cat WHERE tbl_vehicles.veh_advanceLng between (:loc_long-:radius/cos(radians(:loc_lat))*69) and (:loc_long+:radius/cos(radians(:loc_lat))*69) and veh_advanceLat between (:loc_lat-(:radius/69)) and (:loc_lat+(:radius/69)) AND veh_status=:veh_status LIMIT 1");

http://www.mysqltutorial.org/mysql-limit.aspx

或DISTINCT:https://dev.mysql.com/doc/refman/5.7/en/distinct-optimization.html

答案 2 :(得分:0)

为每个类获取一行,您可以使用(假的,例如:min)聚合函数和分组

(假设您有col1,col2,col3而不是*),缺少表模式

$get_veh = $pdo->prepare("SELECT 
               tbl_car_class_category.id_class_cat
                    , min(col1)
                    , min(col2) 
                    , min(col3)
     FROM tbl_car_class_category 
     JOIN tbl_vehicles ON tbl_vehicles.veh_class_id = tbl_car_class_category.id_class_cat
     WHERE tbl_vehicles.veh_advanceLng between (:loc_long-:radius/cos(radians(:loc_lat))*69) 
                    and (:loc_long+:radius/cos(radians(:loc_lat))*69) 
                    and veh_advanceLat between (:loc_lat-(:radius/69)) 
                    and (:loc_lat+(:radius/69)) AND veh_status=:veh_status
                    GROUP BY tbl_car_class_category.id_class_cat");