形如select * from table where riqi between ‘2002-12-24-11-08‘and '2002-12-24-11-20'and otherfield='34'
其中数据库中有两条纪录在其中。
其中一个是  2002-12-24-11-20
另一个是   2002-12-24-11-08
可是我用此SQL语句输出却只显示一条纪录符合条件,这是怎么回事?
另外我想用一个变量来代表 2002-12-24-11-20和2002-12-24-11-08
每次查询只要改变变量的内容就能查询
不用往SQL语句中写具体内容,怎么做呢

解决方案 »

  1.   

    CString strTimeA1,strTimeB1;//假设strTimeA1="2002-12-24";strTimeA2="2002-12-08";CString strSQL;
    strSQL="Select * From Table Where riqi Between '";
    strSQL+=strTimeA1;
    strSQL+="' And '";
    strSQL+=strTimeA2;
    strSQL+=" And otherfield='34'";
      

  2.   

    CString strTimeA1,strTimeB1;//假设strTimeA1="2002-12-24";strTimeA2="2002-12-08";CString strSQL;
    strSQL="Select * From Table Where riqi Between '";
    strSQL+=strTimeA1;
    strSQL+="' And '";
    strSQL+=strTimeA2;
    strSQL+="' And otherfield='34'";
      

  3.   

    你的riqi是否是日期类型,不是转换后就可以了
    select * from table where riqi between cast('2002-.. as datetime) and cast('2002.....' as datetime) and otherfield='34'