简体   繁体   English

SQL 服务器:如何编写包含每个条件不同查询的条件 WHERE 子句?

[英]SQL Server: How to write conditional WHERE clause containing different queries per condition?

I found a bunch of answers on StackOverflow in regards to conditional WHERE clauses, but I wasn't able to find one that was somewhat specific to my issue, so just looking for a little help...我在 StackOverflow 上找到了一堆关于条件 WHERE 子句的答案,但我无法找到一个特定于我的问题的答案,所以只是寻求一点帮助......

My company's website contains all SQL queries written directly within the C# code (I'm not a fan of that, but that's another story) & I'm having an issue trying to update a query that contains multiple conditions in the WHERE clause based on which checkboxes a user checks off on the form.我公司的网站包含直接在 C# 代码中编写的所有 SQL 查询(我不喜欢那个,但这是另一个故事)&我在尝试更新包含基于 WHERE 子句中的多个条件的查询时遇到问题用户在表单上选中的复选框。

提供者类型

Within the form is a set of checkboxes (currently 3 options, but this list will potentially grow).表单中有一组复选框(目前有 3 个选项,但此列表可能会增长)。 For each "Provider Type" selected, an additional condition needs to be added to the WHERE clause but I'm unsure how to best handle this.对于选择的每个“提供者类型”,需要在 WHERE 子句中添加一个附加条件,但我不确定如何最好地处理这个问题。

Here's the code that handles this.这是处理此问题的代码。 ProviderTypesCSV can contain up to 3 values (0,1,2) based on how many options the user checked: ProviderTypesCSV最多可以包含 3 个值 (0,1,2),具体取决于用户检查的选项数量:

if (!String.IsNullOrEmpty(searchOptions.ProviderTypesCSV))
{
    // values are in a csv string.  Split out the csv
    var statuses = searchOptions.ProviderTypesCSV.Split(',');

    if (statuses.Contains("0"))   //Hospitals & Facilities
    {
        strSQL += " AND (<<perform query for 'Hospitals & Facilities'>>)";
    }

    if (statuses.Contains("1"))  //Physicians and Practitioners
    {
        strSQL += " AND (<<perform query for 'Physcians and Practitioners'>>)";
    }

    if (statuses.Contains("2"))   //In-Person Visit Providers
    {
        strSQL += " AND (<<perform query for 'In-Person Visit Providers'>>)";
    }
}

The current issue obviously, is the above query doesn't take into account when multiple options are selected.当前的问题显然是在选择多个选项时上述查询没有考虑到。 When multiple options are selected, the conditional operator needs to be "OR", not "AND", but I'm not sure how to best set this up without making a mess & taking into account add'l options may be added later.选择多个选项时,条件运营商需要为“或“,不是”和“”,但是我不确定如何在不弄乱的情况下最好地设置此设置并考虑到添加'L选项。

Just curious on the best way to set this up.只是好奇设置它的最佳方法。

You can define a List of string and add your conditions, then concat each element with an " OR " delimiter and use it for your AND您可以定义一个字符串列表并添加您的条件,然后使用" OR "分隔符连接每个元素并将其用于您的AND

if (!String.IsNullOrEmpty(searchOptions.ProviderTypesCSV))
    {
        //values are in a csv string.  Split out the csv
        var statuses = searchOptions.ProviderTypesCSV.Split(',');
        
        List<string> conditions = new List<string>();

        if (statuses.Contains("0"))   //Hospitals & Facilities
        {
            conditions.Add("(<<perform query for 'Hospitals & Facilities'>>)");
        }
        if (statuses.Contains("1"))  //Physicians and Practitioners
        {
            sconditions.Add("(<<perform query for 'Physcians and Practitioners'>>)");
        }
        if (statuses.Contains("2"))   //In-Person Visit Providers
        {
            conditions.Add("(<<perform query for 'In-Person Visit Providers'>>)");
        }
        
        strSQL += " AND (" + String.Join(" OR ", list) + ") ";
        
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM