我有2个关注的栏目:
Column 1 - OFFENSE
Column 2 - CALL_SLIP
我想从2019年1月的“攻击”列中查询每个具有“盗窃”,“抢劫”和“空白/空”的记录(无论Call_Slip列中的数据如何)。对于进攻列中的所有空白/空记录,我然后要从具有“ burg bus”或“ rob individ”的Call_Slip列中提取相应的记录。
这是我的查询
From [crimes]
Where date_rept between '1/1/2019' and '1/31/2019'
And offense in ('', 'robbery','burglary')
And Call_Slip in ('burg bus', 'rob individ')
这就是我得到的:
OFFENSE CALL_SLIP
Robbery Rob Individ
Robbery Kidnapping
Burg Bus
Accident
Alarm
Burglary Burg Bus
DWI
Rob Individ
Warrant
Robbery Carjacking
这就是我想要得到的:
OFFENSE CALL_SLIP
Robbery Rob Individ
Robbery Kidnapping
Burg Bus
Burglary Burg Bus
Rob Individ
Robbery Carjacking
如何从offense
列中查询我想要的内容,然后从Call_Slip
列中查询我想要的内容?使用我在上面创建的查询,它同时查询两列,它们不返回burg bus或rob individ之外的Call_Slips
名。进攻可能是抢劫,但Call_Slip名称可能是绑架,没关系。现在,如果进攻为空并且Call_Slip被绑架,那就不行了。
我真的希望这对某人有意义!
答案 0 :(得分:0)
只需使用OR语句包含条件
--SELECT columns
From [crimes]
Where date_rept between '1/1/2019' and '1/31/2019'
And ( (
offense in ('robbery','burglary')
)
OR (
offense in ('') AND Call_Slip in ('burg bus', 'rob individ')
)
)
答案 1 :(得分:0)
怎么样
SELECT *
From [crimes]
Where date_rept between '1/1/2019' and '1/31/2019'
And ((offense = '' And Call_Slip in ('burg bus', 'rob individ'))
OR (offense is null And Call_Slip in ('burg bus', 'rob individ'))
OR (offense in ('robbery','burglary')))