表结构及数据如下:
id   车牌号      地点           经过时间            
1   京A8888     北一路      2009-11-11 09:00:00             
2   京A9999     北一路      2009-11-11 09:01:00             
3   京A7777     北一路      2009-11-11 09:02:00             
4   京A8888     北二路      2009-11-11 09:05:00             
5   京A1234     北二路      2009-11-11 09:05:00             现需要找出既经过"北一路" 又经过 "北二路"的记录,  sql该怎么写呢?
路过的兄弟姐妹给个思路,谢谢先. 

解决方案 »

  1.   

    select id from table where id in (select id from table where 地点 like '北一路') and 地点 like '北二路';
    没测试过..
    试试看。
      

  2.   

    select 车牌号
    from(select 车牌号,count(distinct (case 地点 when '北一路' then 1 '北二路' then 2 end))c from table1
      group by 车牌号)
    where c=2
      

  3.   

    select 车牌号 
    from(select 车牌号,count(distinct (case 地点 when '北一路' then 1 when '北二路' then 2 end))c from table1 
      group by 车牌号) 
    where c=2
      

  4.   

    能否再详细描述下,你要达到的目的?每个车牌+地点是不是唯一的记录,应该不是吧?
    单纯从以上理解,可以如下
    select a.*
    from test a
    where exists (select 1 from test b where b.车牌号='北一路' and b.车牌号=a.车牌号)
    and exists (select 2 from test c where c.车牌号='北二路' and c.车牌号=a.车牌号)
    估计你应该还有其他要求,只是思路,呵呵。
      

  5.   

    要具体记录而不是车牌?
    select *
    from(select t.*,
      count(distinct (case 地点 when '北一路' then 1 '北二路' then 2 end))
      over(partition by 车牌号)c from table1 t

    where c=2
      

  6.   

    纠正:
    select a.*
    from test a
    where exists (select 1 from test b where b.地点='北一路' and b.车牌号=a.车牌号)
    and exists (select 2 from test c where c.地点='北二路' and c.车牌号=a.车牌号)1楼的不错,
    select a.*
    from test a
    where exists (select 1 from test b where b.地点='北一路' and b.车牌号=a.车牌号)
    and a.地点='北二路'
      

  7.   

    select count(*) from 表 where 地点=1路 or 地点=2路
      

  8.   

    SQL> with tt as(
      2  select '1' id,'京A8888' card,'北一路' local,'2009-11-11 09:00:00' time from dual
      3  union all select '2','京A9999','北一路','2009-11-11 09:01:00' from dual
      4  union all select '3','京A7777','北一路','2009-11-11 09:02:00' from dual
      5  union all select '4','京A8888','北二路','2009-11-11 09:05:00' from dual
      6  union all select '5','京A1234','北二路','2009-11-11 09:05:00' from dual
      7  )
      8  select card from tt where local ='北一路'
      9  Intersect
     10  select card from tt where local ='北二路';CARD
    -------
    京A8888
      

  9.   

    SQL> with tt as(
      2  select '1' id,'京A8888' card,'北一路' local,'2009-11-11 09:00:00' time from dual
      3  union all select '2','京A9999','北一路','2009-11-11 09:01:00' from dual
      4  union all select '3','京A7777','北一路','2009-11-11 09:02:00' from dual
      5  union all select '4','京A8888','北二路','2009-11-11 09:05:00' from dual
      6  union all select '5','京A1234','北二路','2009-11-11 09:05:00' from dual
      7  )
      8  select * from tt where card in(
      9  select card from tt where local ='北一路'
     10  Intersect
     11  select card from tt where local ='北二路');ID CARD    LOCAL  TIME
    -- ------- ------ -------------------
    1  京A8888 北一路 2009-11-11 09:00:00
    4  京A8888 北二路 2009-11-11 09:05:00
      

  10.   

    谢谢 1楼 hou1104  6楼lengyunfei006 
    谢谢adebayor 
    用hou1104的方法数据不对lengyunfei006,adebayor 的方法调试中.......