![](/img/trans.png)
[英]Android - How to get from string resources all the elements which contains two or more similar words on their names
[英]How to get values from two tables in which one column in each table contains similar value?
我有兩個表,查詢如下
db.execSQL("CREATE TABLE IF NOT EXISTS allquestions (num_id INTEGER PRIMARY KEY NOT NULL, questions TEXT NOT NULL,catogery TEXT NOT NULL,age INT NOT NULL)" );
db.execSQL("CREATE TABLE IF NOT EXISTS answers (num_id INTEGER PRIMARY KEY NOT NULL,questionid INT NOT NULL,answer TEXT NOT NULL)" );
在這里,所有問題中的num_id包含問題號,並且答案在表(answers)列的答案中。在答案列中,questionid包含答案的問題號。所以我想要的是如何編寫查詢以獲取問題從allquestion及其表答案中得到答案。
我認為您需要它。
select questions,answers from questions que,answers ans where que.num_id=ans.questionid;
您必須在兩個表上應用聯接。
SELECT
allquestions.`num_id`,allquestions.`questions`,answers.questionid,
answers.answer from allquestions JOIN answers ON allquestions.`num_id`
= answers.questionid
嘗試以下方法。 希望您也一樣。
為每個組指定一個row_number,然后使用CASE
語句。
詢問
select t1.questionid,a.questions,
max(case when t1.row_number=1 then t1.answer else 0 end) as answer1,
max(case when t1.row_number=2 then t1.answer else 0 end) as answer2,
max(case when t1.row_number=3 then t1.answer else 0 end) as answer3
from allquestions a
join (
SELECT @row_number:=CASE WHEN @questionid=questionid
THEN @row_number+1
ELSE 1
END AS row_number,
@questionid:=questionid AS questionid,answer
FROM answers,
(SELECT @row_number:=0,@questionid:='') AS t
ORDER BY questionid
)t1
on a.num_id=t1.questionid
group by t1.questionid;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.