WHERE子句中的独占IF语句

时间:2016-09-22 13:48:18

标签: sql tsql

我有以下初始查询。

Select * from Table
where Datum1 < @Datum2
and Employee_Name = Employee_Name

但我希望仅在员工是小组学生的成员时才过滤Datum1 < Datum2。对于非学生员工,我不想过滤Datum1 < @Datum2

我的第一个想法是

Select * from Table
where CASE WHEN Datum1 < @Datum2 END
and Employee_Name = Employee_Name

但这不起作用。所以我检查other SO Solutions引用了where clause in sql if statement,但所有这些问题都解决了我们有if Student then Datum1 < @Datum2 else Datum2 < @Datum2等问题或问题的问题。但是如果条件成立,我希望根本不考虑该陈述。谁可以帮助我?

3 个答案:

答案 0 :(得分:3)

您可以使用Employee_Group语句执行此操作,同时考虑这两种方案。我假设有一个名为Select * From Table Where Employee_Group <> 'Students' Or ( Employee_Group = 'Students' And Datum1 < @Datum2 ) And Employee_Name = @Employee_Name 的字段在问题中未提及,但这应该指向正确的方向:

cell =(FixedCell *)[tableView dequeueReusableCellWithIdentifier:@"FixedCell" forIndexPath:indexPath];


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));
    [cell layoutIfNeeded];
    CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return size.height;
}

答案 1 :(得分:0)

您的条款&#34; Employee_Name = Employee_Name&#34;是没用的。

试试这个:

选择* 从表 其中Datum1&lt; @ Datum2或类别&lt;&gt; &#39;学生&#39;

答案 2 :(得分:0)

如果有一个EmployeeGroup字段,并且您希望过滤EmployeeName并仅在该组不是&#39; Students&#39;那么这就是你的查询的样子:

SELECT * FROM Table
    WHERE EmployeeName = @employeeName 
    AND (Datum < @datum2 OR EmployeeGroup <> 'Students')