CasparUK
asked on
ASP SQL Join Query?
Hi,
This question is leading on from my other question:
https://www.experts-exchange.com/questions/20966025/ASP-SQL-Query.html
(a bit of background... :))
Basically, the above is a SQL keyword search what I need now is a SQL SELECT that can process a form with multi-form fields.
For example the search facility requires a Business record to be search by a few different fields, submitted by a form.
The fields are:
strSearch_Advanced_Members hipStatus = Request.QueryString("SA_Me mbershipSt atus")
strSearch_Advanced_BSO = Request.QueryString("SA_BS O")
strSearch_Advanced_Borough = Request.QueryString("SA_Bo rough")
strSearch_Advanced_Region = Request.QueryString("SA_Re gion")
strSearch_Advanced_Gender = Request.QueryString("SA_Ge nder")
strSearch_Advanced_Age = Request.QueryString("SA_Ag e")
strSearch_Advanced_EthnicO rigin = Request.QueryString("SA_Et hnicOrigin ")
strSearch_Advanced_Employe eNumber = Request.QueryString("SA_Em ployeeNumb er")
strSearch_Advanced_Directo ryClassifi cation = Request.QueryString("SA_Di rectoryCla ssificatio n")
All are type Integer, except
Gender where the submitted paramerers are "male" or "female"
If a option is not changed, from default, then the submission is "AnyContent"
I now need to find a record that matches anyone of the above search criteria. The SQL SELECT line I have so far only does the first one, but am clueless on how to do the rest...?
What I already have:
Dim SQL_Search_Advanced
SQL_Search_Advanced = "SELECT Business.ID,Business.Busin essName,Bu siness.ICF ,Business. CNA,Busine ss.Title_C ode,Busine ss.Forenam e,Business .Surname,B usiness.Te lephone, Membership.BusinessID,Memb ership.Mem bershipSta tus,Member ship.Membe rshipNumbe r,Membersh ip.ExpiryD ate,Member ship.Appoi ntedBSO FROM Business "
SQL_Search_Advanced = SQL_Search_Advanced + "INNER JOIN Membership ON Business.ID = Membership.BusinessID "
If (strSearch_Advanced_Member shipStatus = "AnyContent") Then
SQL_Search_Advanced = SQL_Search_Advanced + "WHERE Membership.MembershipStatu s <> 'blank' "
Else
SQL_Search_Advanced = SQL_Search_Advanced + "WHERE Membership.MembershipStatu s = '"&strSearch_Advanced_Memb ershipStat us&"' "
End If
Any thoughts on how I can integrate the remaining search parameters into the SQL SELECT line?
Caspar
This question is leading on from my other question:
https://www.experts-exchange.com/questions/20966025/ASP-SQL-Query.html
(a bit of background... :))
Basically, the above is a SQL keyword search what I need now is a SQL SELECT that can process a form with multi-form fields.
For example the search facility requires a Business record to be search by a few different fields, submitted by a form.
The fields are:
strSearch_Advanced_Members
strSearch_Advanced_BSO = Request.QueryString("SA_BS
strSearch_Advanced_Borough
strSearch_Advanced_Region = Request.QueryString("SA_Re
strSearch_Advanced_Gender = Request.QueryString("SA_Ge
strSearch_Advanced_Age = Request.QueryString("SA_Ag
strSearch_Advanced_EthnicO
strSearch_Advanced_Employe
strSearch_Advanced_Directo
All are type Integer, except
Gender where the submitted paramerers are "male" or "female"
If a option is not changed, from default, then the submission is "AnyContent"
I now need to find a record that matches anyone of the above search criteria. The SQL SELECT line I have so far only does the first one, but am clueless on how to do the rest...?
What I already have:
Dim SQL_Search_Advanced
SQL_Search_Advanced = "SELECT Business.ID,Business.Busin
SQL_Search_Advanced = SQL_Search_Advanced + "INNER JOIN Membership ON Business.ID = Membership.BusinessID "
If (strSearch_Advanced_Member
SQL_Search_Advanced = SQL_Search_Advanced + "WHERE Membership.MembershipStatu
Else
SQL_Search_Advanced = SQL_Search_Advanced + "WHERE Membership.MembershipStatu
End If
Any thoughts on how I can integrate the remaining search parameters into the SQL SELECT line?
Caspar
ASKER
I see what you are saying, but I think it's not pulling tha data I need...
If I'm reading it right then the data from the database will be
memberstatus = <whatever> or AnyContent
OR
BSO = <whatever> or AnyContent
What I need is the content to be memberstatus AND BSO
For example, here's three records:
1. John:
Memberstatus = Active
BSO = Fred
Borough = Cheshire
2. Adam:
Memberstatus = Inactive
BSO = Caspar
Borough = Surrey
3. Shaun:
Memberstatus = Active
BSO = Caspar
Borough = Derby
So if I did a search on these 3 criteria for:
Membership =Active
BSO=Caspar
Borough=AnyContent
Then I'd expect to only get back the record Shaun and not either of the other two like I think, your script would provide? Cause both non-default (not AnyContent) are satisfied, but only one record both are satisfied.
If I'm reading it right then the data from the database will be
memberstatus = <whatever> or AnyContent
OR
BSO = <whatever> or AnyContent
What I need is the content to be memberstatus AND BSO
For example, here's three records:
1. John:
Memberstatus = Active
BSO = Fred
Borough = Cheshire
2. Adam:
Memberstatus = Inactive
BSO = Caspar
Borough = Surrey
3. Shaun:
Memberstatus = Active
BSO = Caspar
Borough = Derby
So if I did a search on these 3 criteria for:
Membership =Active
BSO=Caspar
Borough=AnyContent
Then I'd expect to only get back the record Shaun and not either of the other two like I think, your script would provide? Cause both non-default (not AnyContent) are satisfied, but only one record both are satisfied.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You will also have to account for when they choose AnyContent for all of the criteria.
Prior to the statement
SQL_Search_Advanced = SQL_Search_Advanced & strWhere
Add this
IF LEN(strWhere) = 0 THEN
SQL_Search_Advanced = LEFT( SQL_Search_Advanced , LEN( SQL_Search_Advanced ) - 7 ) '<--- remove the training " WHERE "
END IF
-------- OR ----------
add this
IF LEN(strWhere) = 0 THEN
strWhere = " 1=1 "
END IF
Either way you would return ALL records from the join.
Prior to the statement
SQL_Search_Advanced = SQL_Search_Advanced & strWhere
Add this
IF LEN(strWhere) = 0 THEN
SQL_Search_Advanced = LEFT( SQL_Search_Advanced , LEN( SQL_Search_Advanced ) - 7 ) '<--- remove the training " WHERE "
END IF
-------- OR ----------
add this
IF LEN(strWhere) = 0 THEN
strWhere = " 1=1 "
END IF
Either way you would return ALL records from the join.
ASKER
Excellent :D
That does the trick :)
Although, I chnaged the below line, it seemed to generate errors before
IF RIGHT( strWhere, 4 ) = " AND " THEN strWhere = LEFT( strWhere, LEN( strWhere ) - 5 ) ' <------ to remove the trailing " AND "
TO:
If RIGHT( strWhere, 4 ) = "AND " THEN
strWhere = LEFT( strWhere, LEN( strWhere ) - 4 )
End If
Thanks again,
C
p.s. I've posted a related question here, if you fancy it? :)
https://www.experts-exchange.com/questions/20973246/ASP-SQL-Age-Query.html
That does the trick :)
Although, I chnaged the below line, it seemed to generate errors before
IF RIGHT( strWhere, 4 ) = " AND " THEN strWhere = LEFT( strWhere, LEN( strWhere ) - 5 ) ' <------ to remove the trailing " AND "
TO:
If RIGHT( strWhere, 4 ) = "AND " THEN
strWhere = LEFT( strWhere, LEN( strWhere ) - 4 )
End If
Thanks again,
C
p.s. I've posted a related question here, if you fancy it? :)
https://www.experts-exchange.com/questions/20973246/ASP-SQL-Age-Query.html
DIM strWhere
strWhere = ""
SQL_Search_Advanced = "SELECT Business.ID,Business.Busin
If (strSearch_Advanced_Member
strWhere = strWhere + "Membership.MembershipStat
Else
strWhere = strWhere + "Membership.MembershipStat
End If
If (strSearch_Advanced_BSO = "AnyContent") Then
strWhere = strWhere & "Membership.BSO <> 'blank' OR "
Else
strWhere = strWhere & "Membership.BSO = '" & strSearch_Advanced_BSO &"' OR "
End If
... etc... then ....
IF RIGHT( strWhere, 4 ) = " OR " THEN strWhere = LEFT( strWhere, LEN( strWhere ) - 4 ) ' <------ to remove the trailing " OR "
SQL_Search_Advanced = SQL_Search_Advanced & strWhere
HTH