简体   繁体   中英

Query Logic best approach

i'm after the data obtained by my two queries plus any other data from the driving table. I'm using the following code but have a feeling my results are wrong.

select * from(
select * from tbl_a a  
inner join tbl_b b on (a.id = b.id and a.col_a = b.col_b and a.col_c = '1')

union all

select * from tbl_a a  
inner join tbl_b b on (a.col_a = b.col_b and a.col_c = '1')
where (1=1)
and a.id <> b.id
and a.start_time <= b.u_start_time
and a.end_time >= b.u_end_time

union all

select * from tbl_a a  
where a.another_id 
NOT IN ( -- either query above)

) results;  

I'd just like to know if this makes sense or how I could possibly simplify some of this...

Here is query for the first 2 unions,and it is not clear what is the third union condition

SELECT * 
FROM
tbl_a a 
left join tbl_b b on b.id = a.id and b.col_b = a.col_a
left join tbl_b b1 on a.col_a= b1.col_b and a.id<>b1.id and a.start_time<=b1.u_start_time and a.end_time>=b1.u_end_time
WHERE
a.col_c=1
and COALESCE(b.id,b1.id) is not null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM