select * 
from test0130_main,test0130_sub 
where test0130_main.id *= test0130_sub.main_id
and 
      (
( pe >= @Begin and pe < @End )
or
( pe < @Begin and ae >= @Begin)
       )
我没仔细看,逻辑好象是这样.

解决方案 »

  1.   

    where test0130_main.id *= test0130_sub.main_id
          and 
          (   ( pe >= @Begin and pe < @End )  --加了括号
              or
              ( pe < @Begin and ae >= @Begin)
          )
      

  2.   

    不好意思,没写仔细,实际上是加了括号结果也是一样的不对,结果如下select * from test0130_main
    select * from test0130_subdeclare @Begin int
    declare @End int
    set @Begin = -1
    set @End = 1select * 
    from test0130_main,test0130_sub 
    where test0130_main.id *= test0130_sub.main_id
    and 
    ( (pe >= @Begin and pe < @End )
    or
    ( pe < @Begin and ae >= @Begin))
    第一个查询
    id       ps       pe
    1 5 7
    2 1 2
    第二个
    id       main_id  as        ae
    1 1 9 10
    不明白的查询
    id       ps       pe       sub.id    main_id   as       ae
    1 5 7 NULL NULL NULL NULL
    2 1 2 NULL NULL NULL NULL
      

  3.   

    select * 
    from test0130_main left join test0130_sub on
     test0130_main.id = test0130_sub.main_id
    where 
    (( pe >= @Begin and pe < @End )
    or
    ( pe < @Begin and ae >= @Begin))
      

  4.   

    结果是对的,但是同样的条件写在from子句和where子句的区别是什么,
    为什么会这样??
    谢谢