I want to get all the records showing only ID's which didn't go through stutus=2 but have been canceled from specific statuses. Tables are A_ORDERS_LOG and A_ORDERS.
So, each ID can go through various statuses. I am interested to see only those ID's which have been canceled from certain statuses and haven't been through status=2.
I have tried this
SELECT *
FROM A_ORDERS_LOG
WHERE ( STATUS1 = 0 AND STATUS2 = 11 )
OR ( STATUS1 = 1 AND STATUS2 = 11 )
OR ( STATUS1 = 3 AND STATUS2 = 11 )
OR ( STATUS1 = 4 AND STATUS2 = 11 )
OR ( STATUS1 = 41 AND STATUS2 = 11 )
OR ( STATUS1 = 42 AND STATUS2 = 11 )
OR ( STATUS1 = 43 AND STATUS2 = 11 )
OR ( STATUS1 = 5 AND STATUS2 = 11 )
OR ( STATUS1 = 6 AND STATUS2 = 11 )
AND EXISTS (
SELECT STATUS1,
STATUS2
FROM A_ORDERS_LOG
WHERE ( STATUS1 != 2 OR STATUS2 != 2 )
AND ID IN (
SELECT ID
FROM A_ORDERS
)
)
);
Status2=11 - means canceling and status1 is the status from which it was canceled.
Seems you need one exists
and one not exists
while tiding up the filtering statuses just after the where
clause:
select *
from a_orders_log o
where status1 in ( 0, 1, 3, 4, 5, 6, 41, 42, 43 ) and status2 = 11
and not exists (select 0
from a_orders_log o3
where 2 in ( status1 , status2 )
and exists ( select 1 from a_orders o2 where o2.id = o3.id ) );
Just adding more simplicity by using join instead of exists.
select *
from a_orders_log o
where status1 in ( 0, 1, 3, 4, 5, 6, 41, 42, 43 ) and status2 = 11
and not exists (select 0
from a_orders_log aoo join a_orders ao on (ao.id=aoo.id)
where 2 in ( status1 , status2 ) )
@barbaros, It will be appreciated if you can please check it for correctness.
Cheers!!
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.