如何选择除一个值之外的一列中的所有数据?

时间:2013-07-22 11:50:54

标签: c# database ms-access-2010

我写了这个c#代码来从学生信息中选择card_id,除了一个card_id,但我从网上读到除了条款不能访问...我怎么做这个请帮帮我?

ol_com.CommandText = "select [card_id] from student_info except select [card_id] from student_info where [card_id] = '"+update_st_card.Text+"'";
                reader = ol_com.ExecuteReader();
                if (reader.HasRows)
                {
                    up_st_lbl2.Text = "message";
                }

4 个答案:

答案 0 :(得分:3)

我认为您不必要地进行复杂查询。

可以使用NOT IN

轻松解决
ol_com.CommandText = "select [card_id] from student_info 
where [card_id] not in ('"+update_st_card.Text+"')";

答案 1 :(得分:1)

你插入没有问题,所以请尝试这个简单的。 update_st_card.Text只有一个card_Id。

ol_com.CommandText = "select [card_id] from student_info 
where [card_id] ='"+update_st_card.Text+"'";
reader = ol_com.ExecuteReader();
string str=string.Empty();
if (reader.HasRows)
{
  //Update here
    str="Update student_info set mobile_no='Your new value to set'
             where [card_id] ='"+update_st_card.Text+"'";

}
else
{
  //insert here
    str="Your Insert command";
}
ol_com.CommandText = str;
ol_com.ExecuteNonQuery();

答案 2 :(得分:0)

我认为这将解决您的问题,我们可以不在任何地方使用,sql server,nhibernate

ol_com.CommandText = "select [card_id] from student_info where [card_id] not in ( select [card_id] from student_info where [card_id] = '"+update_st_card.Text+"'");
reader = ol_com.ExecuteReader();
if (reader.HasRows)
{
   up_st_lbl2.Text = "message";
}

答案 3 :(得分:0)

我认为你可以使用NOT IN

您的查询可以写成,

select [card_id] 
from student_info 
where [card_id] NOT IN 
 (select [card_id] from student_info where [card_id] = '"+update_st_card.Text+"'")