我有两个查询按预期单独工作。当我使用union来一次获取两个查询的结果时,不会获取第二个查询的行。我的查询简化如下:
( 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'
整个查询的结果与第一部分相同。