0

需要帮助将此查询的输出组合在一起。如何完成??TQ

select facility, route, operation, script_id 
from F_ROUTEOPER
where facility = 'A01' and operation in ('6910','7976')
AND src_erase_date is null
and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)  
AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'
AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')


select facility, route, operation, script_id 
from F_ROUTEOPER
where facility = 'A01' and operation in ('6912','7976') 
AND src_erase_date is null
and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)  
AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'
AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')

select facility, route, operation, script_id 
from F_ROUTEOPER
where facility = 'A01' and operation in ('7344','7976') 
AND src_erase_date is null
and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)  
AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'
AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')

select facility, route, operation, script_id 
from F_ROUTEOPER
where facility = 'A01' and operation in ('8344','7976')
AND src_erase_date is null
and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)  
AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'
AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
4

5 回答 5

2

将所有值合并operation到一个 IN... 即

operation in ('6910','7976','6912','7344','8344')

你的其他条件完全一样。完整的查询

select facility, route, operation, script_id 
  from F_ROUTEOPER
 where facility = 'A01'
   and operation in ('6910','7976','6912','7344','8344')
   AND src_erase_date is null
   and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)  
   AND (route NOT LIKE '9EL%' AND
        route NOT LIKE '9TB%' AND
        route NOT LIKE 'BLB%' AND
        route NOT LIKE 'BWR%' AND
        route NOT LIKE 'CRL%')
于 2012-11-05T09:13:16.650 回答
2

关键字 UNION 应该非常适合您。看看这里的文档 - http://dev.mysql.com/doc/refman/5.0/en/union.html

于 2012-11-05T09:15:11.040 回答
1

这四个查询似乎是相同的 excpet 谓词AND operation In ..,您可以将这个谓词组合成四个查询,如下所示:

select facility, route, operation, script_id 
from F_ROUTEOPER
where facility = 'A01' and operation in ('6910','7976', '6912', '8344','7976')
...

但是,您可以使用UNION(implicit distinct 或UNION ALL根据需要组合来自不同查询的结果。

于 2012-11-05T09:14:24.247 回答
1

将 的值合并为operation一个IN

select facility, route, operation, script_id 
from F_ROUTEOPER
where facility = 'A01' and operation in ('6910','7976', '6912', '7344', '8344') 
    AND src_erase_date is null 
    AND (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)  
    AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%' AND route NOT LIKE 'BLB%' AND 
         route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
于 2012-11-05T09:15:04.917 回答
0

如果所有查询都具有相同数量的列、相同的列名和相同的数据类型,则使用UNIONUNION ALL

select col1,col2,col3 from table1 
union
select col4 as col1,col5 as col2,col6 as col3 from table 2
于 2012-11-05T09:12:18.200 回答