I build a simple question-answer system.
In my database, there are three tables:
question (
id int
question varchar(200)
answer_id int /* foreign key mapping to answer.id */
);
answer (
id int
answer varchar(500)
)
question_elements (
id int
seq int /*vocabulary in question location */
question_id int /** foreign key mapping to question.id */
vocabulary varchar(40)
)
Now I have a question:
What approach should a company adopt when its debt ratio is higher than 50% and wanna continue to get funding ?
So in table question, a record is:
question {
id: 1,
question:"What approach should a company adopt when its debt ratio is higher than 50% and wanna continue to get funding ?",
answer_id:1
}
In table question_elements
question_elements [
{
id: 1,
seq: 1,
question_id: 1,
vocabulary: "what"
},
{
id: 2,
seq: 2,
question_id: 1,
vocabulary: "approach"
},
{
id: 3,
seq: 3,
question_id: 1,
vocabulary: "should"
},
{
id: 4,
seq: 4,
question_id: 1,
vocabulary: "a"
},
{
id: 5,
seq: 5,
question_id: 1,
vocabulary: "company"
},
{
id: 6,
seq: 6,
question_id: 1,
vocabulary: "adopt"
},
{
id: 7,
seq: 7,
question_id: 1,
vocabulary: "when"
},
....
....
{
id: 19,
seq: 19,
question_id: 1,
vocabulary: "get"
},
{
id: 20,
seq: 20,
question_id: 1,
vocabulary: "funding"
}
]
Now, when a user inputs:
What action does a company should do when it wanna get more funding with high debt ratio
My idea is to split the above statement into a string list, and execute a SQL query in order to count the matched string in table question_elements by giving the above string list.
What is the SQL statement in PostgreSQL ?