两个表:”两个表以CID关联“A表 ,有三个字段: ID, CID,Time 比如:
---------------------------------------------------------
ID     cID             Time
---------------------------------------------------------
1       4         2006-9-1 14:36:16
2       7         2006-9-1 14:37:32
3       8         2006-9-1 15:38:44
4       10        2006-9-1 15:39:34
5       12        2006-9-1 15:40:21
6       14        2006-9-1 16:43:36
7       16        2006-9-1 16:44:35
8       19        2006-9-1 16:45:11
9       20        2006-9-1 17:47:01
10      22        2006-9-1 17:50:32
11      27        2006-9-1 17:51:36
B表, 两个字段:   CID,data比如:
---------------------------------------------------------
      cID             data
---------------------------------------------------------
       4               ABC
       8               BCS
       10              ABC
       12              BCS
       14              CDW
       16              DVF
       19              VBG
       22              ABC
       27              BCS
---------------------------------------------------------”两个表以CID关联“
请问,如何写一个SQL查询语句查询 B表中有几个数据是发生在 2006-9-1 15:00:00 到 2006-9-1 18:00:00

解决方案 »

  1.   


    select b.* from B left join A on B.cID=A.cID where A.time between '2006-9-1 15:00:00' and '2006-9-1 18:00:00'
      

  2.   

    select count(A.ID) from A,B where A.CID=B.CID and A.[Time] between '2006-9-1 15:00:00' and '2006-9-1 18:00:00'
      

  3.   

    select count(*) as 记录数 from B 
    where CID in
    (
    select CID from A where [Time] between '2006-9-1 15:00:00' and '2006-9-1 18:00:00'
    )
      

  4.   

    select * from B表 b where exists(select 1 from A表 a where a.CID=b.CID and a.Time BETWEEN '2006-9-1 15:00:00' and '2006-9-1 18:00:00')