Hello, I'm trying to find a solution to a simple search engine's SQL query problem. At the moment my query has a flaw that makes it leave out valid records if the user selects "Any" from a dynamic option (drop down list) menu instead of actually selecting an individual statement. I assigned the "Any" selection to have the value of '%'.
************* Start summary of HTML end *************
The gist of the html end of the search engine goes like this:
[ Drop List 1 ]
[ Drop List 2 ] [ Drop List 3 ]
The search is on a database of cataloged images. The result of the search should be a table of images that match the user requested parameters.
Drop List 1 is simply a list of all people shown in the pictures. Its default value is "Any" with a real passed value of '%'. An actual select example other than "Any" could be "Doe, John". Drop List 1 values are in the database as "KeyWords.Keyword".
Drop List 2 and 3 are simply organizational categories the images may fall under. Drop List 2 is the parent menu of Drop List 3. Changing Drop List 2 dynamically changes Drop List 3. The default for these menus again are "Any" with a real passed value of '%'. An actual select example other than "Any" could be: for Drop List 2, "Event" and for Drop List 3, "Weddings". Drop List 3 values are in the database as "Attributes.Data".
The search result should then be any images of John Doe at a wedding.
(There's also a date range but that part isn't the problem so leaving it out.)
************* END summary of HTML end *************
************* Start My current SQL query *************
SELECT DISTINCT Pictures.PicFilename, Pictures.PicId, Pictures.PicDate
...truncated statement
FROM Pictures, Attributes, KeyWords
WHERE (Attributes.Data LIKE 'mnu_ValueList') AND KeyWords.Keyword LIKE 'mnu_keywordList' AND (Attributes.PicId = Pictures.PicId) AND (Attributes.PicId = KeyWords.PicId) AND (YEAR(Pictures.PicDate) BETWEEN 'txtDateRngBegin' AND 'txtDateRngEnd')
ORDER BY Pictures.PicDate
************* END My current SQL query *************
Problem: My problem stems from the fact that it's possible for an image to be in the database that does not have any related keyword (person) or attribute data (category or event). One example that fails is a picture of a person with no related "Event" (Attribues.Data). In this case my WHERE statement causes the picture to be omitted because the image does not even exist in the Attribute table. The user selection of "Any" passes the value of '%' While this does produce the expected results if the picture just happens to be in the Attribute table it however fails if it is not.
"Any" causes (Attributes.Data LIKE 'mnu_ValueList') to be
(Attributes.Data LIKE '%'). This selects all entries in the Attributes table but since the picture doesn't have a related entry in the Attributes table the "AND" statement causes the image to be omitted.
Is there a way to modify the SQL statement to fix this?
************* Start summary of HTML end *************
The gist of the html end of the search engine goes like this:
[ Drop List 1 ]
[ Drop List 2 ] [ Drop List 3 ]
The search is on a database of cataloged images. The result of the search should be a table of images that match the user requested parameters.
Drop List 1 is simply a list of all people shown in the pictures. Its default value is "Any" with a real passed value of '%'. An actual select example other than "Any" could be "Doe, John". Drop List 1 values are in the database as "KeyWords.Keyword".
Drop List 2 and 3 are simply organizational categories the images may fall under. Drop List 2 is the parent menu of Drop List 3. Changing Drop List 2 dynamically changes Drop List 3. The default for these menus again are "Any" with a real passed value of '%'. An actual select example other than "Any" could be: for Drop List 2, "Event" and for Drop List 3, "Weddings". Drop List 3 values are in the database as "Attributes.Data".
The search result should then be any images of John Doe at a wedding.
(There's also a date range but that part isn't the problem so leaving it out.)
************* END summary of HTML end *************
************* Start My current SQL query *************
SELECT DISTINCT Pictures.PicFilename, Pictures.PicId, Pictures.PicDate
...truncated statement
FROM Pictures, Attributes, KeyWords
WHERE (Attributes.Data LIKE 'mnu_ValueList') AND KeyWords.Keyword LIKE 'mnu_keywordList' AND (Attributes.PicId = Pictures.PicId) AND (Attributes.PicId = KeyWords.PicId) AND (YEAR(Pictures.PicDate) BETWEEN 'txtDateRngBegin' AND 'txtDateRngEnd')
ORDER BY Pictures.PicDate
************* END My current SQL query *************
Problem: My problem stems from the fact that it's possible for an image to be in the database that does not have any related keyword (person) or attribute data (category or event). One example that fails is a picture of a person with no related "Event" (Attribues.Data). In this case my WHERE statement causes the picture to be omitted because the image does not even exist in the Attribute table. The user selection of "Any" passes the value of '%' While this does produce the expected results if the picture just happens to be in the Attribute table it however fails if it is not.
"Any" causes (Attributes.Data LIKE 'mnu_ValueList') to be
(Attributes.Data LIKE '%'). This selects all entries in the Attributes table but since the picture doesn't have a related entry in the Attributes table the "AND" statement causes the image to be omitted.
Is there a way to modify the SQL statement to fix this?