Lets start at the begining. What are those *asterixes* doing around answer? You might just be able to get away with :-
SELECT * FROM Grade_Theory WHERE 'The funcdamental particle is called
+a quark, Bob!' LIKE '%'||Answer||'%'
But I have never needed to use this reverse syntax.
To be perfectly honest with you this looks less like a perl question and more like an advanced database design question.
Ideally you need to understand the importance of the key words in the answers.
So let us try and build up a dynamic query that tries to find important words. Your initial SQL starts off :-
SELECT * FROM Grade_Theory
Although this sounds ok it can create massive performace issues in the future when some adds many large columns to the table. Always specify exactly what you are after.
SELECT RowID, Question,Answer
FROM Grade_Theory
Now comes the tricky part finding the matching answer. Now we do not know exactly how many words are relivent so we need to try and find a best match. Perl can help by generating the where clause.
my @words = split /\W+/, 'The funcdamental particle is called a quark,
+ Bob!';
my @parts = ();
for (@words) {
push @parts, "Answer like '%$_%'";
}
my $where_clause = join (" or\n",@parts);
my $sql = "SELECT RowID, Question,Answer
FROM Grade_Theory
WHERE $where_clause";
print "$sql\n";
This will result in the following type of query.
SELECT RowID, Question,Answer
FROM Grade_Theory
WHERE Answer like '%The%' or
Answer like '%funcdamental%' or
Answer like '%particle%' or
Answer like '%is%' or
Answer like '%called%' or
Answer like '%quark%' or
Answer like '%Bob%'
You can improved this by removing short/irrelvent words from the word list.
my @words = split /\W+/, 'The funcdamental particle is called a quark,
+ Bob!';
my @long_words=();
for (@words) {
push @long_words, $1 if (/^(\w{3,})$/);
}
my @parts = ();
for (@long_words) {
push @parts, "Answer like '%$_%'";
}
my $where_clause = join (" or\n",@parts);
my $sql = "SELECT RowID, Question,Answer
FROM Grade_Theory
WHERE $where_clause";
print "$sql\n";
This will result in the following resulting query.
SELECT RowID, Question,Answer
FROM Grade_Theory
WHERE Answer like '%The%' or
Answer like '%funcdamental%' or
Answer like '%particle%' or
Answer like '%called%' or
Answer like '%quark%' or
Answer like '%Bob%'
If you want to go any further you really need a completely different design of database. If you have a table that contains words and a many to many relationship between that and your question table you could then do a far more interesting query. But this is PERL monks so I will lay of the database theory.
Hope it helps
UnderMine
|