[英]Select statment from multiple tables, with variable input
I have two tables: AreaCode
and EquipmentNumber
. 我有两个表:
AreaCode
和EquipmentNumber
。
+------------------------------------+
| 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) |
+------------------------------------+
I want to extract EqNum.Number
, Eq.Type
and AreaCd.Code
. 我想提取
EqNum.Number
, Eq.Type
和AreaCd.Code
。 The trouble is, that the query is populated by input from a form, so the search restrictions are variable. 问题在于,查询是由来自表单的输入填充的,因此搜索限制是可变的。 I have created 3 seperate queries similar to these:
我创建了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 + "';";
Which all work fine on their own, provided the user is only searching for one column ( Code
, Number
, or Type
) at a time, but I need the capability to search for one, two, or all three columns at once. 如果用户一次只搜索一列(
Code
, Number
或 Type
),那么所有这些都靠自己很好地工作,但是我需要能够一次搜索一列,两列或所有三列。
I have tried using OR
, LIKE
, multiple selection, I've even tried casting int nCode
as a char to use the %
wildcard, but I just can't find something that works. 我尝试使用
OR
, LIKE
,多选,甚至尝试将int nCode
为char以使用%
通配符,但我只是找不到有效的方法。
QUESTION: Can someone help me join these three queries to search the tables for any combination of all three felds: EqNum.Number
, EqNum.Type
and AreaCd.Code
that will refine the search result when more fields are added? 问题:有人可以帮助我加入这三个查询,以便在表中搜索所有三个字段的任意组合:
EqNum.Number
, EqNum.Type
和AreaCd.Code
,当添加更多字段时,这些字段可以优化搜索结果? (ie a search for EqNum.Type
will yield more results than a search for EqNum.Number
, EqNum.Type
and AreaCd.Code
) (即,搜索
EqNum.Type
将比搜索EqNum.Number
, EqNum.Type
和AreaCd.Code
产生更多的结果)
I think this is what you are after : 我认为这就是您所追求的:
Just added a simple trick; 只是添加了一个简单的技巧;
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 + "')"
OR using LIKE
: 或使用
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 + "%')"
See an example at SQL Fiddle 在SQL Fiddle上查看示例
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)
To learn how to use parameters with ADO.NET, click here . 要了解如何在ADO.NET中使用参数, 请单击此处 。
Setting parameters would look something like this: 设置参数如下所示:
command.Parameters["@Number"].Value = (string.IsNullOrEmpty(number) ? (object) DBNull.Value : number);
Try this 尝试这个
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 + "'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.