1

我有两个按预期分别工作的查询。当我一次使用联合来获取两个查询的结果时,不会从第二个查询中获取任何行。我的查询简化如下:

( select 
    tmp.inv_id AS inv_id,
    tmp.name AS inv_name,
    tmp.level AS inv_level,
    'archive' AS alarm_tpye,
    log.start_time AS start_time,
    log.end_time AS end_time 
  FROM tmp_raw_inventory AS tmp
  INNER JOIN log_alarm_archive AS log ON  tmp.inv_id=log.inv_id
  WHERE 
    log.start_time between STR_TO_DATE($date1,'%Y-%m-%d') and STR_TO_DATE($date2,'%Y-%m-%d')  OR
    log.end_time between STR_TO_DATE($date1,'%Y-%m-%d') and STR_TO_DATE($date2,'%Y-%m-%d')
) 

UNION

( select 
    tmp.inv_id AS inv_id,
    tmp.name AS inv_name,
    tmp.level AS inv_level,
    'active' AS alarm_tpye,
    log.alarm_time AS start_time,
    STR_TO_DATE($date1,'%Y-%m-%d') AS end_time 
  FROM tmp_raw_inventory AS tmp
  INNER JOIN log_alarm AS log ON  tmp.inv_id=log.inv_id
  WHERE 
    log.alarm_time between STR_TO_DATE($date1,'%Y-%m-%d') and STR_TO_DATE($date2,'%Y-%m-%d')
) 

$date1 和 $date2 是以正确形式传递的字符串日期参数。第一个查询的结果来了,但第二个查询的结果没有来。这些行不是唯一的,因此我使用了UNION. 在这次失败之后,我进行了测试,UNION ALL但结果没有改变。

您的帮助将不胜感激。

更新: 第一部分的结果如下:

inv_id, inv_name        , inv_level, alarm_type, start_time           , end_time
234231, 'inventory name', 'high'   , 'archive' , '2013-02-10 05:38:03', '2013-02-12 09:36:26'
234231, 'inventory name', 'high'   , 'archive' , '2013-02-10 16:10:18', '2013-02-10 17:03:13'
234231, 'inventory name', 'high'   , 'archive' , '2013-02-10 20:08:12', '2013-02-10 21:17:37'

第二部分的结果如下:

inv_id, inv_name        , inv_level, alarm_type, start_time           , end_time
234231, 'inventory name', 'high'   , 'active' , '2013-02-10 23:27:03', '2013-02-11 00:00:00'

整个查询的结果与第一部分相同。

4

0 回答 0