SQL 根据列条件检索行
SQL retrieve rows based on column condition
假设我有以下 tables
订单Table
OrderNo CategoryID CountryID ServiceTypeID
100 1 3 1
200 2 5 2
300 3 4 4
400 1 2 9
1个服务类型可能属于多个类别类型
类别Table
ID Name ServiceTypeID
1 x 1
2 x 2
3 x 1
服务类型table
ID Name
1 xx
2 xx
3 xx
追踪Table
OrderNo CountryID TrackingTypeID
100 2 3
200 1 4
100 3 2
400 5 1
200 2 6
已审核Table
OrderNo
300
100
200
我想编写符合以下要求的查询
订单必须属于 serviceTypeID = 1 或 2
并且如果 orderNo 有一个 categoryID = 1
我希望仅在以下情况下检索该记录
有一个跟踪记录 table
orderNo 与 Order table
中的 countryID 相同
并且如果该订单号在跟踪中没有跟踪类型 id (0,7,1) table
Else 对于具有任何其他类别的所有其他订单,不包括不属于的订单
到 serviceTypeID = (1,2)
我希望只有在以下情况下才能检索该记录
该 orderNo 的现有记录在
已审核 table
并且如果该订单号在跟踪中没有跟踪类型 id (0,7,1) table
所以基本上基于以上要求,结果应该是这样的
OrderNo CategoryID StationID
100 1 3
200 2 5
select DISTINCT top 10000 o.orderNo , o.categoryID , o.serviceTypeid
,o.countrtId , Tracking.countryId
from Order o
join Tracking on o.orderNo = Tracking.orderNo
where
(o.CategoryID in (1 ) and o.countryId = Tracking.countryId
and
exists (select 1
from tracking t
where t.orderNo = o.orderNo and t.countryId =
o.countryId
)
)
OR
(o.categoryID in (select id from Category where ServiceTypeid in (7,8) and
ID not in (56 , 65)
) and
exists (select 1
from Reviewed r
where r.orderNo = o.orderNo
)
)
AND not exists
( select 1
from tracking t
WHERE T.orderNo = o.orderNo
and t.TrackingTypeID in (0 , 7 ,25))
该查询似乎 return 只有 ID 为 1 的订单,即使它的 trackingTypeID = 0,7,25
您可以使用 exists
和布尔逻辑:
select o.*
from orders o
where
(
category_id = 1
and exists (select 1 from tracking t where t.order_no = o.order_no and t.country_id = o.country_id)
)
or (
category_id = 2
and exists (select 1 from reviewed r where r.order_no = o.order_no)
)
根据您的条件,您可以使用布尔逻辑 exists
:
select o.*
from orders o
where (o.categoryid = 1 and
exists (select 1
from tracking t
where t.orderno = o.orderno and t.CountryID = o.CountryID
)
) or
(o.categoryid = 2 and
exists (select 1
from reviewed r
where r.orderno = o.orderno
)
) ;
根据您的条件,此查询将给出相应的结果:
Select o.OrderNo,o.CategoryID,o.CountryID as StationID
from Order o
inner join Tracking t on t.OrderNo = o.OrderNo and t.CountryID = o.CountryID
where o.CategoryID = 1 or (o.CategoryID = 2
and exists (Select * from Reviewed where OrderNo = o.OrderNo))
假设我有以下 tables
订单Table
OrderNo CategoryID CountryID ServiceTypeID
100 1 3 1
200 2 5 2
300 3 4 4
400 1 2 9
1个服务类型可能属于多个类别类型
类别Table
ID Name ServiceTypeID
1 x 1
2 x 2
3 x 1
服务类型table
ID Name
1 xx
2 xx
3 xx
追踪Table
OrderNo CountryID TrackingTypeID
100 2 3
200 1 4
100 3 2
400 5 1
200 2 6
已审核Table
OrderNo
300
100
200
我想编写符合以下要求的查询
订单必须属于 serviceTypeID = 1 或 2
并且如果 orderNo 有一个 categoryID = 1
我希望仅在以下情况下检索该记录 有一个跟踪记录 table orderNo 与 Order table
中的 countryID 相同并且如果该订单号在跟踪中没有跟踪类型 id (0,7,1) table
Else 对于具有任何其他类别的所有其他订单,不包括不属于的订单 到 serviceTypeID = (1,2) 我希望只有在以下情况下才能检索该记录 该 orderNo 的现有记录在 已审核 table
并且如果该订单号在跟踪中没有跟踪类型 id (0,7,1) table
所以基本上基于以上要求,结果应该是这样的
OrderNo CategoryID StationID
100 1 3
200 2 5
select DISTINCT top 10000 o.orderNo , o.categoryID , o.serviceTypeid
,o.countrtId , Tracking.countryId
from Order o
join Tracking on o.orderNo = Tracking.orderNo
where
(o.CategoryID in (1 ) and o.countryId = Tracking.countryId
and
exists (select 1
from tracking t
where t.orderNo = o.orderNo and t.countryId =
o.countryId
)
)
OR
(o.categoryID in (select id from Category where ServiceTypeid in (7,8) and
ID not in (56 , 65)
) and
exists (select 1
from Reviewed r
where r.orderNo = o.orderNo
)
)
AND not exists
( select 1
from tracking t
WHERE T.orderNo = o.orderNo
and t.TrackingTypeID in (0 , 7 ,25))
该查询似乎 return 只有 ID 为 1 的订单,即使它的 trackingTypeID = 0,7,25
您可以使用 exists
和布尔逻辑:
select o.*
from orders o
where
(
category_id = 1
and exists (select 1 from tracking t where t.order_no = o.order_no and t.country_id = o.country_id)
)
or (
category_id = 2
and exists (select 1 from reviewed r where r.order_no = o.order_no)
)
根据您的条件,您可以使用布尔逻辑 exists
:
select o.*
from orders o
where (o.categoryid = 1 and
exists (select 1
from tracking t
where t.orderno = o.orderno and t.CountryID = o.CountryID
)
) or
(o.categoryid = 2 and
exists (select 1
from reviewed r
where r.orderno = o.orderno
)
) ;
根据您的条件,此查询将给出相应的结果:
Select o.OrderNo,o.CategoryID,o.CountryID as StationID
from Order o
inner join Tracking t on t.OrderNo = o.OrderNo and t.CountryID = o.CountryID
where o.CategoryID = 1 or (o.CategoryID = 2
and exists (Select * from Reviewed where OrderNo = o.OrderNo))