我有一个包含to列的表:Question和Answer。我想要一个查询,其结果是一个列,其中包含Question和它各自的Answer。
举个例子:
表"Question_Answer“
Q1, A1
Q2, A2
Q3, A3
Q4, A4查询输出,列名为"Question_Answer_Result“
Q1
A1
Q2
A2
Q3
A3
Q4
A4我尝试了以下命令:
select "Question_Answer_Result"
from (select "Question_Answer"."Question"
from "Question_Answer"
union all
select "Question_Answer"."Answer"
from "Question_Answer"
)但我收到一条消息“命令意外结束”。
正确的SQL命令是什么?
谢谢。
发布于 2020-01-04 00:04:48
您从子查询中选择了什么?select从子查询结果中查找列"Question_Answer_Result“;这样的列不存在。唯一的一栏是“问题”。
发布于 2020-01-04 03:23:18
初始select中缺少列引用。正如我在第一个响应中提到的,“问题”列是union子查询的结果。您需要按名称选择该列(或者*就足够了),然后将其重命名。
选择问题' question_answer‘from (从question_answer联合中选择问题all select answer from question_answer)
编辑:这是SQLite,可以获得准确的输出
select question 'question_answer'
from (
select question from question_answer
union all
select answer from question_answer
) x
order by substr(question,length(question-1),1),substr(question,1) desc请注意,输出的顺序将不会如您所示。
https://stackoverflow.com/questions/59581621
复制相似问题