【发布时间】: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 是否支持参数的通用值。
【问题讨论】: