从多个表中选择带有变量输入的语句

时间:2014-03-11 04:18:49

标签: c# mysql sql

我有两个表:AreaCodeEquipmentNumber

+------------------------------------+
| AreaCd                             |
|------------------------------------|
| AreaID INT NOT NULL AUTO_INCREMENT |
| Code INT                           |
| Name CHAR(30)                      |
| Comments TEXT                      |
| PKEY (AreaID)                      |
+------------------------------------+
+------------------------------------+
| EqNum                              |
|------------------------------------|
| EqID INT NOT NULL AUTO_INCREMENT   |
| AreaID INT                         |
| Number CHAR(5)                     | 
| Type CHAR(10)                      |
| PKEY (EqID)                        |
| FKEY (AreaID) REF AreaCode(AreaID) |
+------------------------------------+

我想提取EqNum.NumberEq.TypeAreaCd.Code。问题是,查询是由表单输入填充的,因此搜索限制是可变的。我创建了3个与此类似的单独查询:

"SELECT Number, Type, Code FROM EqNum, AreaCd " +
"WHERE EqNum.AreaId = AreaCd.AreaId AND Code = " + int nCode + ";";

"SELECT Number, Type, Code FROM EqNum, AreaCd " +
"WHERE EqNum.AreaId = AreaCd.AreaId AND Number = '" + string sNumber + "';";

"SELECT Number, Type, Code FROM EqNum, AreaCd " +
"WHERE EqNum.AreaId = AreaCd.AreaId AND Type = '" + string sType + "';";

如果用户一次只搜索一列(CodeNumber Type),那么这一切都能正常运行,但我需要能够一次搜索一,二,所有三列。

我尝试过使用ORLIKE,多次选择,我甚至尝试将int nCode作为char来使用%通配符,但我可以找不到有用的东西。

问题:有人可以帮我加入这三个查询来搜索表格中所有三个字母的任意组合:EqNum.NumberEqNum.TypeAreaCd.Code这些将改进搜索添加更多字段时的结果(即搜索EqNum.Type会产生比搜索EqNum.NumberEqNum.TypeAreaCd.Code

3 个答案:

答案 0 :(得分:1)

我认为这就是你所追求的目标:

刚刚添加了一个简单的技巧;

if(string.IsNullOrEmpty(sNumber))
    sNumber="$$$";               //If sNumber is empty, then selection using sNumber= '$$$' will return nothing.
if(string.IsNullOrEmpty(sType))
    sType="$$$"                 //If sType is empty, then selection using sType = '$$$' will return nothing.
"SELECT Number, Type, Code FROM EqNum, AreaCd " +
"WHERE EqNum.AreaId = AreaCd.AreaId 
AND 
  (Code = " + int nCode + " 
   OR Number = '" + string sNumber + "' 
   OR Type = '" + string sType + "')"

使用LIKE

if(string.IsNullOrEmpty(sNumber))
    sNumber="$$$";             //If sNumber is empty, then selection using sNumber LIKE '%$$$%' will return nothing.
if(string.IsNullOrEmpty(sType))
    sType="$$$"                //If sType is empty, then selection using sType LIKE '%$$$%' will return nothing.
"SELECT Number, Type, Code FROM EqNum, AreaCd " +
"WHERE EqNum.AreaId = AreaCd.AreaId 
AND 
  (Code = " + int nCode + "
   OR Number LIKE '%" + string sNumber + "%' 
   OR Type LIKE '%" + string sType + "%')"

请参阅SQL Fiddle

上的示例

答案 1 :(得分:1)

SELECT e.Number, e.Type, a.Code
FROM EqNum e INNER JOIN AreaCd a
ON e.AreaId = a.AreaId
WHERE (@Number IS NULL OR e.Number = @Number)
AND (@Type IS NULL OR e.Type = @Type)
AND (@Code IS NULL OR a.Code = @Code)

要了解如何在ADO.NET中使用参数click here

设置参数如下所示:

command.Parameters["@Number"].Value = (string.IsNullOrEmpty(number) ? (object) DBNull.Value : number);

答案 2 :(得分:0)

试试这个

  SELECT Number, Type, Code FROM EqNum, AreaCd " +
   "WHERE EqNum.AreaId = AreaCd.AreaId AND 
   isnull(Code,0) = " + int nCode + " or
    isnull(Number,0) = '" + string sNumber + "' or isnull(Type,0) = '" + string sType + "'