如何结合这两个查询

时间:2013-05-28 10:59:54

标签: mysql tree parent-child hierarchical-data

此查询适用于活动

  $query1 = "select t.entity_id, t.parent_id, t.length,gu.measurement_unit_id
    from tree t
    left join units u
    on t.parent_id = u.region_id
    and u.activity_id IN (some activity_id)
    left join product_grid_units gu
    on u.id = gu.unit_id
    WHERE t.entity_id IN (some region_id)";

此查询适用于区域

      $query2 =  "select t.entity_id, t.parent_id, t.length,gu.measurement_unit_id
    from tree t
    left join units u
    on t.parent_id = u.activity_id
    and u.region_id IN (some region_ids)
    left join product_grid_units gu
    on u.id = gu.unit_id
    WHERE t.entity_id IN ("some activity_ids")";

所以我想把这两个问题结合起来......任何人都可以帮忙吗?...

1 个答案:

答案 0 :(得分:0)

使用UNION(隐式不同)或UNION ALL,如下所示:

select t.entity_id, t.parent_id, t.length,gu.measurement_unit_id
from tree t
left join units u on t.parent_id = u.region_id
                  and u.activity_id IN (some activity_id)
left join product_grid_units gu on u.id = gu.unit_id
WHERE t.entity_id IN (some region_id)
UNION ALL
select t.entity_id, t.parent_id, t.length,gu.measurement_unit_id
from tree t
left join units u  on t.parent_id = u.activity_id
                  and u.region_id IN (some region_ids)
left join product_grid_units gu on u.id = gu.unit_id
WHERE t.entity_id IN ("some activity_ids")