If it matters, I’m using Firebird 2.1 database.
I have three tables, one with keywords, one with negative keywords, and the other with required keywords. I need to be able to filter the data so the output has just the keywords that meat the stipulation of not being in the negative keyword list, and IF there are any required words, then it will require the results to have those keywords in the end result.
The tables are very similar, the field in the tables that I would be matching against are all called keyword.
I don’t know SQL very well at all. I’m guessing it would be something like SELECT keyword from keywordstable where keyword in requiredkeywordstable and where NOT in negativekeywordstable
Just a side note, The required keywords table could be empty which would mean there are no required keywords.
Any help would be appreciated.
Example Of Tables:
KeywordsTable
-Keywords varchar 255
RequiredKeywordsTable
-Keywords varchar 255
NegativeKeywordsTable
-Keywords varchar 255
Example Data:
KeywordsTable
Cat
Dog
Mouse
Horse
House
With Nothing set in the Negative and Required Keywords Tables then the output would simply be the Keywords Table data unchanged.
IF RequiredKeywordsTable has the value of Car, Cat, Dog then the output would be and Cat Dog
If NegativeKeywordsTable has the value of Horse and requiredkeywords was empty then the output of the Keywords table would be cat, dog, mouse, House.
etc..
-Brad
Your specification is a bit hazy. It would help if you provided some schema. Is the keywords table just words or is it a list of keywords for a given entity? What happens if there exists at least one RequiredKeyword but not all Keywords are required? Should the non-required keywords show or should only required keywords show in that scenario? If both required and non-required keywords should be returned, then how does the list of required keywords affect the outcome? Here are some possible solutions:
Scenario 1:
Scenario 2:
Scenario 3:
Addition
Given your additional information, here is how you can solve the problem. First, based on what you said, I’m presuming the schema looks something like:
If the
Keywordscolumn is the only column, I would make it not nullable and the primary key. This ensures that you do not have duplicates and lets us rely on the fact that the column is not nullable to check for non-existence. The problem is significantly more difficult to solve if the Keywords column is nullable in the NegativeKeywords and/or RequiredKeywords table.