如何组合这些SQL查询以便它可以显示一个输出?

时间:2012-11-05 09:10:51

标签: sql

帮助需要将此查询的输出组合在一起。如何完成?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%')

5 个答案:

答案 0 :(得分: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%')

答案 1 :(得分:2)

关键字UNION应该对您有用。 请查看此处的文档 - http://dev.mysql.com/doc/refman/5.0/en/union.html

答案 2 :(得分:1)

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

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

但是,如果您愿意,可以使用UNION(隐式不同或UNION ALL来组合来自不同查询的结果。

答案 3 :(得分: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%')

{{1}}

答案 4 :(得分:0)

如果所有查询的列数相同且列名相同且数据类型相同,则使用UNIONUNION ALL

select col1,col2,col3 from table1 
union
select col4 as col1,col5 as col2,col6 as col3 from table 2