Ben
Ben

Reputation: 2523

Select statment from multiple tables, with variable input

I have two tables: AreaCode and 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. 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:

"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.

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.

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? (i.e. a search for EqNum.Type will yield more results than a search for EqNum.Number, EqNum.Type and AreaCd.Code)

Upvotes: 1

Views: 326

Answers (3)

Raging Bull
Raging Bull

Reputation: 18747

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:

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

Upvotes: 1

jmcilhinney
jmcilhinney

Reputation: 54417

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.

Setting parameters would look something like this:

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

Upvotes: 1

odlan yer
odlan yer

Reputation: 771

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 + "'

Upvotes: 0

Related Questions