从select命令中排除记录

时间:2016-04-28 10:03:58

标签: c# sql asp.net database

我有一个选择命令,它工作得非常好。

我想排除SchoolID =?的一条记录?

我该怎么做?

SelectCommand="SELECT [School], [CenShort], [Center], [NameLevel],
[Tuition_Tuition] FROM [ASchools] 
WHERE (([SectID] = ?) AND (([LevelNo] = ?)
      OR ([LevelNo] = ?) OR ([LevelNo] = ?) OR ([LevelNo] = ?)
      OR ([LevelNo] = ?)) AND (([TSchools_Tuition] = ?) OR ([TSchools_Tuition] = ?) 
      OR ([TSchools_Tuition] = ?)) AND ([Gender] = ?))">

4 个答案:

答案 0 :(得分:0)

试试这个

SelectCommand="SELECT [School], [CenShort], [Center], [NameLevel],
[Tuition_Tuition] FROM [ASchools] 
WHERE (([SectID] = ?) AND (([LevelNo] = ?)
      OR ([LevelNo] = ?) OR ([LevelNo] = ?) OR ([LevelNo] = ?)
      OR ([LevelNo] = ?)) AND (([TSchools_Tuition] = ?) OR ([TSchools_Tuition] = ?) 
      OR ([TSchools_Tuition] = ?)) AND ([Gender] = ?) and school<>?)">

答案 1 :(得分:0)

我建议将附加条件附加到where子句的末尾。还可以使用语法IN (valueA,valueB,valueC)使您的查询更易于阅读。

SelectCommand=
@"SELECT [School], [CenShort], [Center], [NameLevel], [Tuition_Tuition] 
FROM [ASchools]  
WHERE [SectID] = ? 
  AND [LevelNo] in(?,?,?,?) 
  AND [TSchools_Tuition] in (?,?,?) 
  AND [Gender] = ? 
  AND [SchoolID] <> ?"

答案 2 :(得分:0)

在SQL中你也可以这样做:

WHERE ...
AND NOT SchoolID = ?

答案 3 :(得分:0)

如果SchoolID是您的主要搜索参数,那么您应该改变您的where子句,如下所示:WHERE SchoolID&lt;&gt;?和(......所有其他条件)。 所以现在你的查询将首先排除具有某些SchoolID的记录,然后继续进行其他条件。