I’m using the following query to return all records where at least 2 conditions match (provided by Quassnoi).
SELECT *
FROM (
SELECT ContentID
FROM (
SELECT ContentID
FROM VWTenantPropertiesResults
WHERE ContentStreet = 'Holderness Road'
UNION ALL
SELECT ContentID
FROM VWTenantPropertiesResults
WHERE ContentTown = 'Hull'
UNION ALL
SELECT ContentID
FROM VWTenantPropertiesResults
WHERE ContentPostCode = 'HU'
) qi
GROUP BY
ContentID
HAVING COUNT(*) >= 2
) q
JOIN VWTenantPropertiesResults r
ON r.ContentID = q.ContentID
WHERE ContentBedrooms BETWEEN 1 AND 4
AND ContentPrice BETWEEN 50 AND 500
ORDER BY
ContentPrice
The problem is that it seems to work when searching for Street and Town (returns all matching properties with the requested street and town), but not when searching for Street and Postcode (returns no results). To get the search for Street and Postcode to work (returning results), I had to remove the following lines;
UNION ALL
SELECT id
FROM VWTenantPropertiesResults
WHERE ContentTown = 'Hull'
But then obviously the Town and Postcode or Town and Street searches don’t work because i had removed the above 4 lines to get the Street and Postcode search to work.
I wondered if anyone could offer some help with this?
Thank you.
I’m not sure you should be enforcing the ‘at least two conditions’ criteria down in the database as you should probably never have knowledge of which two have been filled in. Perhaps instead this might work for you – it’s a pattern I use quite often and should cope with any combination of criteria (I’m assuming this is inside a stored proc!):
To call this from your ASP page you’ll want some code something like this (this may need a bit of debugging, my ADO & VBScript for ASP is pretty rusty!):
This ought to work for any combination of parameters, whether you enter none, some or all of them!