我構建了一個簡單的問答系統。SQL:如何查詢一組數據並計算給定字符串列表中匹配的字符串數量
在我的數據庫中,有三個表:
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)
)
現在我有一個問題:
What approach should a company adopt when its debt ratio is higher than 50% and wanna continue to get funding ?
表問題
因此,記錄是:
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
}
在表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"
}
]
現在,當用戶輸入:
What action does a company should do when it wanna get more funding with high debt ratio
我的想法是爲了通過給上述計算表question_elements匹配的字符串上面的語句分割成一個字符串列表,並執行一個SQL查詢字符串列表。
什麼是PostgreSQL中的SQL語句?
你是使用json字段還是你向我們顯示數據的方式? –
看起來你有兩個問題。一個是用'「」執行分割,另一個是查看有多少匹配。 –
[在Postgres中將列拆分成多行]可能的副本(http://stackoverflow.com/questions/29419993/split-column-into-multiple-rows-in-postgres) –