【问题标题】:Using Select and Where parameter = any\dontcare in MySQL with C#使用 C# 在 MySQL 中使用 Select 和 Where 参数 = any\dontcare
【发布时间】:2013-08-20 13:33:09
【问题描述】:

在我的基于 MySQL 的 C# 代码中,我必须在我的数据库上实现和搜索过滤器,所以我编写了一个 select * from table where(参数条件列表); 问题是有时我想要特定参数的默认“全选”。喜欢 select * from table where type=(any\don't care);

我想要显示所有类型的所有不同行而不过滤。如果我省略了 where 子句,也会发生同样的情况;我不能省略 where 子句,因为我的查询结构会被破坏。 .整个额外或缺少的“和”导致我使用字符串生成器连接查询。我会在下面发布我的代码,以防有人有更好的方法;

        str.Append("Select * From ");
        str.Append(" recording ");
        str.Append(" WHERE ");



        switch (type)
        {
            case "Audio": str.Append(" and Type = " + 1 + " ");
                break;
            case "Video": str.Append(" and Type = " + 2 + " ");
                break;
            case "VoIP": str.Append(" and Type = " + 3 + " ");
                break;
            default: <**SUGGESTION HERE**> 
                break;
        }

        if (!(channelname == ""))
        {
            str.Append("and ChannelName = '" + channelname + "' ");
        }

        if(!(channel == "All"))
        {
            str.Append(" and ChannelId = '" + channel + "' ");
        }



        if (archive == "true")
        {
            str.Append(" and Archive = " + true + " ");
        }
        else if (archive == "false")
        {
            str.Append(" and Archive = " + true + " ");
        }

        str.Append(" and StartTime > '" + from + "' and ");

        str.Append("StartTime < '" + to + "' ");
        if (duration > 0)
        {
            str.Append(" and (SELECT TIMESTAMPDIFF(SECOND,EndTime,StartTime))" + durationtype + " " + duration);
        }

        str.Append(" ;");
        sqltext = str.ToString();

在这里,如果你可以在 switch 语句中建议一个可以显示所有类型的语句,该语句具有与“type=anything”等效的逻辑,就可以解决问题。而且我知道“输入(1,2,3等)”。 .但是这些字段是用户可创建的,所以稍后会出现很多我想要一个通用的解决方案,而且我知道我可以在里面使用另一个 select 语句,比如 "type in (select bla bla)" 。 .我只是想知道 MySQL 是否支持参数的通用值。

【问题讨论】:

    标签: c# mysql sql


    【解决方案1】:

    这样改

    str.Append(" WHERE 1=1 ");
    

    现在您的查询的其余部分可以保持不变。你的查询会变成这样

    select * from yourTable where 1=1 and condition1 and condition2 ....
    

    【讨论】:

      【解决方案2】:

      构建 sql 查询的经典解决方案是 (WHERE 1=1),它适用于每一行。

      【讨论】:

        【解决方案3】:

        我建议在 where 中添加 1=1

            str.Append("Select * From ");
            str.Append(" recording ");
            str.Append(" WHERE 1=1 ");
        
        
        
            switch (type)
            {
                case "Audio": str.Append(" and Type = " + 1 + " ");
                    break;
                case "Video": str.Append(" and Type = " + 2 + " ");
                    break;
                case "VoIP": str.Append(" and Type = " + 3 + " ");
                    break;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-30
          • 2011-01-12
          • 1970-01-01
          • 2013-07-11
          • 2015-07-26
          相关资源
          最近更新 更多