表名假设为 :学生
列名假设为 : 姓名   学号 
select * 
from 学生
where 姓名 = 'L1' and 学号 = 'x' 问 :  x 是什么的时候 上边查询语句 能与 
select * 
from 学生
where 姓名 = 'L1'
达到一样的结果  ?  意思就是忽略“学号”这个查询条件。

解决方案 »

  1.   

    select *  
    from 学生
    where 姓名 = 'L1' and 学号 = 学号
      

  2.   

    declare @参数一  varchar(20)
    declare @参数二  varchar(20)select * 
    from 学生
    where 姓名 = isnull(@参数一,姓名) and 学号 = isnull(@参数二,学号) 
      

  3.   


    如果是 
    select *  
    from 学生
    where 姓名 = 'L1' and 学号 = '学号'就完美解决问题了  
    可是在程序中是要加上‘’的,在一个多条件查询的环境中,如果某一个条件是空的  就出现上面我问的问题了。
      

  4.   

    就这个呀,即使为空也对.select *  
    from 学生
    where 姓名 = 'L1' and 学号 = 学号
      

  5.   

    也许没理解我说的问题 是这样的 :
    strSQL = "select * from 学生 where 学号 = '" + textBox1.Text.Trim() + "'and 姓名 = '" + textBox2.Text.Trim() + "'";
    当textBox1 或者textBox2 为空的时候就查不到该查到的内容。
    我试图定义两个变量来代替这个textBox里的内容,当哪个为空的时候 给它赋值,可是赋什么值可以查到该查到的内容呢?
      

  6.   

    看了呀 ,可是没看懂 ,貌似isnull是替换SQL中列是空的项。反正是不太明白。
    看这个 : http://zhidao.baidu.com/question/80798036.html 
    跟我的问题差不多 ,可是我用C#
      

  7.   


    select *   
    from 学生
    where  ((姓名= isnull(条件值,姓名)) or (''=isnull(条件值,姓名)))
    and ((学号= isnull(条件值,学号)) or (''=isnull(条件值,学号))) 
      

  8.   


    --1 可以在程序中拼SQL
    strSQL = "select * from 学生 where 1=1 ";if textBox1.Text.Trim() <> "" then
    strSQL = strSQL + " and 学号 = '" + textBox1.Text.Trim() + "'"
    end ifif textBox2.Text.Trim() <> "" then
    strSQL = strSQL + " and 姓名 = '" + textBox2Text.Trim() + "'"
    end if--2 用SQL处理
    select *  from 学生
    where 姓名 = case when @p1 = '' then 姓名 else @p1 end 
     and 学号 = case when @p2 = '' then 学号 else @p2 end 
    如OK,记得结贴哦
      

  9.   

    这样的问题,可用like 
    select *  
    from 学生
    where 姓名 = 'L1' and (学号 like 'x' or 学号='x')
    当x为%时可满足LZ条件
      

  10.   

    这个问题解决了 。 谢谢大家 。 
    解决办法:StringBuilder strSQL1 = new StringBuilder("select * from 学生 where 姓名 = ");
    strSQL1.Append("L1");
    if(x!=""){strSQL1.Append(" and 学号 = ").Append(x);}
    else strSQL1.Append("");
    string strSQL = strSQL1.ToString();这个办法完美解决了我的问题,当然这样要求姓名为主键,不能为空。 即使后面很多条件为空,也只需要为每个条件做个IF判断 。
    谢谢各位。 特别是2# 14 # 15# …………