I have an advanced search form that offers many ways to filter your search. Here's a simplified idea (does not include entering keyword text or searching for a date range or other selection menus):
Topic: <select><option>any</option><option>all</option></select> [] Aging [] Environment [] Health [] Hunger [] Poverty Document type: <select><option>any</option><option>all</option></select> [] Case Study [] Policy Brief [] Whitepaper
If someone selects "any" when they select more than one type of topic or document, the request should include, for example, topic = "Aging" OR topic = "Health".
If someone selects "everything" when he selects more than one type or type of document, the request should include, for example, topic = "Aging" AND topic = "Health".
The default is AND between these different filters. Therefore, when searching for all documents classified in the "Aging" section and all documents classified as a technical document, the query: topic = "Aging" And doctype = "whitepaper".
Problem: We have a query that works when a search is run for "any". But when the search is "everything", according to the MySQL EXPLAIN command, we have the "impossible WHERE" .: (
Here is a query that works when someone selects "any" for the subject and type of document:
SELECT DISTINCT * FROM research JOIN link_resource_doctype ON link_resource_doctype.resource_id = research.research_id JOIN doctype ON doctype.id = link_resource_doctype.doctype_id JOIN link_resource_issue_area ON link_resource_issue_area.resource_id = research.research_id JOIN issue_area ON issue_area.id = link_resource_issue_area.issue_area_id WHERE approved = '1' AND (doctype.identifier = 'case_study' OR doctype.identifier = 'whitepaper') AND (issue_area.identifier = 'aging' OR issue_area.identifier = 'health')
And here is the same query that does not work when someone selects βeverythingβ for the topic and type of document (this also does not work if someone selects only the topic or only the type of document):
SELECT DISTINCT * FROM research JOIN link_resource_doctype ON link_resource_doctype.resource_id = research.research_id JOIN doctype ON doctype.id = link_resource_doctype.doctype_id JOIN link_resource_issue_area ON link_resource_issue_area.resource_id = research.research_id JOIN issue_area ON issue_area.id = link_resource_issue_area.issue_area_id WHERE approved = '1' AND (doctype.identifier = 'case_study' AND doctype.identifier = 'whitepaper') AND (issue_area.identifier = 'aging' AND issue_area.identifier = 'health')
Possible solution, but there is a problem: I came across this message in Stackoverflow - Select a line belonging to several categories - which contains a query that, I think, can solve our problem when someone selects "everything." Here he is:
SELECT DISTINCT * FROM research JOIN link_issue_area ON link_issue_area.resource_id = research.research_id JOIN link_doctype ON link_doctype.resource_id = research.research_id WHERE issue_area.identifier IN ('aging', 'health') AND doctype_id.identifier IN ('case_study', 'whitepaper') GROUP BY research.research_id HAVING COUNT(DISTINCT issue_area.identifier) = 2 AND COUNT(DISTINCT doctype.identifier) = 2
Problem: This query works for "any" as well as for "everything", except for one problem. Say the document is classified into Aging, Health, and Poverty, but the seeker only checked Aging and Health. A document that is classified into two topics, as well as Poverty that has not been verified, will not appear in the list of search results. I think this is because of HAVING COUNT (DISTINCT issue_area.identifier) ββ= 2 - 2 excludes any document, actually has COUNT, which is more than 2. Is there any work for this? Or is the best request to use here?
Any ideas, ideas, help are greatly appreciated! Thanks!
Here's the SQLfiddle that gets it all: http://sqlfiddle.com/#!2/847362/1