select 车次 from 一个表 where 站名 in ('a','b') group by 车次 having count(distinct 站名)=2

解决方案 »

  1.   

    select distinct 车次 
    from table  a 
    where exists(select top 1  1 from table where 车次=a.车次 and 站名='a')
     and 车次 ='b'
      

  2.   

    select a.车次
    from 表 a inner join 表 b on a.车次=b.车次 and (a.站名='a' and b.站名='b' or (a.站名='b' and b.站名='a' ))
      

  3.   

    Select distinct 车次 from yourTable
    where 车次 in (select 车次 from yourTable where 站名=a )
    and 车次 in (select 车次 from yourTable where 站名=b )
      

  4.   

    select distinct 车次 
    from table  a 
    where exists(select top 1  1 from table where 车次=a.车次 and 站名='a')
     and 站名 ='b'
      

  5.   

    这样就OK了!
    select 车次 from 一个表 where 站名 in ('a','b') group by 车次
      

  6.   

    select 车次 from 车次表 where 站名 in ('a','b')
    group by 车次
    是啊,我刚试过.
      

  7.   

    ckp(surge) :
    假如a 和 b 之间还有其他车站,    select 车次 from 车次表 where 站名 in ('a','b')  group by 车次
     
    就不正确了!
      

  8.   

    to 楼上. 
    这样的话经过a.b任一站点的车次均显示.不合楼主要求select 车次 from tablename 
    where 站点 in('a','b')
    group by 车次
    having count(车次)=2