如何在sql查询中获得正确的结果?

时间:2013-08-21 07:07:44

标签: c# sql sql-server-2008

我想通过我的Membership表查询以获取某个成员的结果,但是我得到了错误的结果。这是我的疑问:

  select MemNo, MemDOB, MemberType, MemShrBal
  From Membership
  Where MemberType = 1 or MemberType = 2 
  and MemDOB > '1973/12/31' 
  and MemShrBal = 250.00

任何人都可以帮我找出原因:除了正确的成员记录外,我还会获得MemShrBal超过250.0和{{1}的成员记录下面' 1973'?

1 个答案:

答案 0 :(得分:9)

你需要一些括号:

select MemNo, MemDOB, MemberType, MemShrBal
  From Membership
  Where (MemberType = 1 or MemberType = 2)
  and MemDOB > '1973/12/31' 
  and MemShrBal = 250.00

andor绑定得更紧,所以没有括号,它就是

  MemberType = 1

  or
      MemberType = 2
      and MemDOB > '1973/12/31' 
      and MemShrBal = 250.00