Avatar of harris9999
harris9999
Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

SQL Staement

Having trouble getting my SQL statement right.
Have a products page and filters down the left hand side. Currently it works if 2 filters is selected on the left then it either of those filters are matched then the product is displayed.

I want it to be that if both filters are matched then the product is displayed.
Main Product Table
 Main Product TableMain Filters table
 Main Filter Table
Currently MY SQL Statement is (which shows product if either filter mathed) and works:
Select DISTINCT Main.ID, Main.* From Main INNER JOIN MainFilters ON Main.Id = MainFilters.MainId WHERE Available=1 AND (CategoryIds = '3' OR CategoryIds LIKE '3,%' OR CategoryIds LIKE '%, 3,%' OR CategoryIds LIKE '%, 3') AND (((FilterId=4) AND ( FilterValues = '9' OR FilterValues Like '%9,%' OR FilterValues Like '%, 9')) OR ((FilterId=9) AND ( FilterValues = '43' OR FilterValues Like '%43,%' OR FilterValues Like '%, 43'))) Order By DisOrder, Main.ID Desc, Title

But I am having trouble to get both match.
Tried this SQL but it doesn't work.
Select DISTINCT Main.ID, Main.* From Main INNER JOIN MainFilters ON Main.Id = MainFilters.MainId WHERE Available=1 AND (CategoryIds = '3' OR CategoryIds LIKE '3,%' OR CategoryIds LIKE '%, 3,%' OR CategoryIds LIKE '%, 3') AND (((FilterId=4) AND ( FilterValues = '9' OR FilterValues Like '%9,%' OR FilterValues Like '%, 9')) AND ((FilterId=9) AND ( FilterValues = '43' OR FilterValues Like '%43,%' OR FilterValues Like '%, 43'))) Order By DisOrder, Main.ID Desc, Title

How should I build my SQL Statement?
ASPMicrosoft Access

Avatar of undefined
Last Comment
harris9999

8/22/2022 - Mon