我有两个表,下面给出了查询
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)" );
此处allqestions中的num_id包含问题编号,答案列在表格(答案)列答案中。在答案栏中,questionid包含答案的问题编号。所以我想要的是如何编写查询从表格答案中获得所有问题及其答案的问题。
答案 0 :(得分:1)
我认为你需要它。
select questions,answers from questions que,answers ans where que.num_id=ans.questionid;
答案 1 :(得分:0)
您必须在两个表上应用连接。
SELECT
allquestions.`num_id`,allquestions.`questions`,answers.questionid,
answers.answer from allquestions JOIN answers ON allquestions.`num_id`
= answers.questionid
答案 2 :(得分:0)
尝试以下方法。我希望你也在寻找同样的东西
为每个组分配一个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;