I have two tables the queries are given below
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)" );
Here the num_id in allqestions contains the question number and the answers are there in the table(answers) column answer.In the answer column the questionid contains the question number for the answer.So what I want is how to write the query to get questions from allquestion and its answers from table answer.
我认为您需要它。
select questions,answers from questions que,answers ans where que.num_id=ans.questionid;
You have to apply the join on both tables.
SELECT
allquestions.`num_id`,allquestions.`questions`,answers.questionid,
answers.answer from allquestions JOIN answers ON allquestions.`num_id`
= answers.questionid
Try the following. I hope you are looking for the same.
Give a row_number for each group, then use a CASE
statement.
Query
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.