注册 登录
编程论坛 ASP.NET技术论坛

if语句如何写

lukebc 发布于 2017-01-02 19:30, 2961 次点击
这个if语句,我不管输入的是一个日期,还是两个日期,它查询的结果都是>开始日期and 小于结束日期的结果
(select distinct(进仓编号),进仓时间 from ck_进仓主表 zb inner join ck_进仓明细 mx on zb.进仓主键=mx.进仓主键 and 进仓时间>='2016-1-1 00:00:00'and 进仓时间<=' 23:59:59' order by 进仓编号),我想要的是当我输入一个日期的时候查询结果为大于开始日期或小于结束日期的结果,if语句应该如何改

if (ma1.Success)
            {
                if (pd.Text == "0")
                {
                   selectSql = "select  distinct(进仓编号),进仓时间 from  ck_进仓主表 zb inner join ck_进仓明细 mx on zb.进仓主键=mx.进仓主键"
                              + " and CONVERT(varchar(100), 进仓时间, 23)=CONVERT(varchar(100), GETDATE(), 23) and 仓库='浦东' " + kh
                              + " order by 进仓编号";
                    Label1.Text = "浦东仓库当日记录";
                }
                else
                {
                    selectSql = "select  distinct(进仓编号),进仓时间 from  ck_进仓主表 zb inner join ck_进仓明细 mx on zb.进仓主键=mx.进仓主键"
                                  + " and CONVERT(varchar(100), 进仓时间, 23)=CONVERT(varchar(100), dateadd(day,-" + pd.Text + ",getdate()), 23) and 仓库='浦东'" + kh
                                  + " order by 进仓编号";
                    Label1.Text = "浦东仓库第前" + pd.Text + "天记录";

                }
            }

            else if (ma2.Success)
            {
                time1 = pd.Text.Trim();
                cond = cond + "and 进仓时间>='" + time1 + " 00:00:00'";
                Label1.Text = "浦东仓库" + pd.Text + "至今的记录";

if (ma3.Success)
                    {
                        time2 = ys.Text.Trim();
                        cond = cond + "and 进仓时间<='" + time2 + " 23:59:59'";
                        Label1.Text = "浦东仓库" + pd.Text + "至" + ys.Text + "的记录";
                        
                    }

                }
            if (cond != "")
            {
                selectSql = "select  distinct(进仓编号),进仓时间 from  ck_进仓主表 zb inner join ck_进仓明细 mx on zb.进仓主键=mx.进仓主键 " + cond + kh +
             "order by 进仓编号";
                Response.Write(selectSql);
                Response.End();
            }
1 回复
#2
lionv2017-03-06 12:51
sql 语句的条件分开写
   select 。。。。。where 1=1
   if 开始日期!=""
   {
     strwhere= and 日期》"开始日期"
   }
   if 结束日期!=""
   {
     strwhere=strwhere+ and 日期《"结束日期"
   }
  sql+strwhere
1