SQL Where子句更改导致意外结果

时间:2016-09-19 19:14:44

标签: sql-server

我遇到查询中Where子句的问题。为什么第一个查询的结果与第二个查询的结果不同?

第一个查询是:

select distinct
    [Event Code] = evt_code,
    [Event Category] = etc_code,
    [Acronym] = evt_acronym_ext,
    [Remit To Sort Name] = cst_sort_name_dn,
    [Rec #] = cst_recno,
    [Honorarium Amount] = case when fac_honorarium_amount_ext is null then 0.00 else fac_honorarium_amount_ext end,
    [Speaker type] = spt_code
from 
    ev_event (nolock)
inner join 
    ev_event_ext (nolock) on evt_key_ext = evt_key 
inner join 
    ev_event_category (nolock) on etc_key = evt_etc_key and etc_delete_flag = 0
inner join 
    ev_event_faculty (nolock) on fac_evt_key = evt_key and fac_delete_flag = 0
inner join 
    ev_event_faculty_ext (nolock) on fac_key_ext = fac_key --and fac_honorarium_amount_ext is not null and fac_honorarium_amount_ext > 0.0
inner join 
    co_customer (nolock) on cst_key = fac_cst_key and cst_delete_flag = 0
inner join 
    ev_event_speaker (nolock) on spk_fac_key = fac_key and spk_delete_flag = 0
inner join 
    ev_event_speaker_type (nolock) on spt_key = spk_spt_key and spt_delete_flag = 0
where 
    evt_code like '416%A' or evt_code like '516%A'
    and spt_code in ('Ed Fdn Speaker', 'Ed Fdn Author', 'Ed Fdn Conf Chair')
order by 
    evt_code

第二个查询是:

select distinct
    [Event Code] = evt_code,
    [Event Category] = etc_code,
    [Acronym] = evt_acronym_ext,
    [Remit To Sort Name] = cst_sort_name_dn,
    [Rec #] = cst_recno,
    [Honorarium Amount] = case when fac_honorarium_amount_ext is null then 0.00 else fac_honorarium_amount_ext end,
    [Speaker type] = spt_code
from 
    ev_event (nolock)
inner join 
    ev_event_ext (nolock) on evt_key_ext = evt_key 
inner join 
    ev_event_category (nolock) on etc_key = evt_etc_key and etc_delete_flag = 0
inner join 
    ev_event_faculty (nolock) on fac_evt_key = evt_key and fac_delete_flag = 0
inner join 
    ev_event_faculty_ext (nolock) on fac_key_ext = fac_key --and fac_honorarium_amount_ext is not null and fac_honorarium_amount_ext > 0.0
inner join 
    co_customer (nolock) on cst_key = fac_cst_key and cst_delete_flag = 0
inner join 
    ev_event_speaker (nolock) on spk_fac_key = fac_key and spk_delete_flag = 0
inner join 
    ev_event_speaker_type (nolock) on spt_key = spk_spt_key and spt_delete_flag = 0
where 
    (evt_code like '416%A' 
     and spt_code in ('Ed Fdn Speaker', 'Ed Fdn Author', 'Ed Fdn Conf Chair') )
    or 
    (evt_code like '516%A' 
     and spt_code in ('Ed Fdn Speaker', 'Ed Fdn Author', 'Ed Fdn Conf Chair') )
order by 
    evt_code

2 个答案:

答案 0 :(得分:1)

您正在评估的第一个查询' 416%A'通过它自己。你只能用括号括起来以避免这种情况......

where (evt_code like '416%A' or evt_code like '516%A') AND
spt_code in ('Ed Fdn Speaker', 'Ed Fdn Author', 'Ed Fdn Conf Chair')

答案 1 :(得分:0)

数据库正在解释第一个where子句,如下所示:

evt_code like '416%A' 
and (spt_code in ('Ed Fdn Speaker', 'Ed Fdn Author', 'Ed Fdn Conf Chair') 
     or evt_code like '516%A')
    )

简单英语:如果evt_code'416%A'类似,那么如果spt_code中的任何一个属于3个术语中的任何一个,或者evt_code类似于516%A,则满足where子句,或者都。

说数据库添加它自己的括号在技术上并不准确,但这是一个很好的方式来描绘它。