我有一个只会产生2行的查询。每行包含一个问题及其匹配答案。我想要做的是有一行4列。如何才能做到这一点?我可以将值连接成一个字符串,但我宁愿不这样做。
有人能为我提供一个例子吗?这就是我所追求的:
Q1 A1 Q2 A2
Some question Some answer Another question Another answer
这是我的查询
SELECT question, answer FROM
user_question AS uq
Join question AS q ON q.question_id = uq.question_id
答案 0 :(得分:1)
当然可以,您只需要查询同一个表两次。您应该使用AS关键字,这样您就可以确定在SELECT和WHERE部分中使用了哪个版本的表:
SELECT a.question as q1,
a.answer as a1,
b.question as q2,
b.answer as a2
FROM user_question as a,
user_question as b
WHERE a.question_id = 1001
AND b.question_id = 1002;
答案 1 :(得分:0)
如何使用UNION
?
SELECT iQuestion, iAnswer
FROM
(SELECT Q1 as iQuestion, -- change to orig column name
A1 as iAnswer, -- change to orig column name
questionID
FROM user_question INNER JOIN question
ON user_question.question_id = question.question_id
UNION
SELECT Q2 as iQuestion, -- change to orig column name
A2 as iAnswer, -- change to orig column name
questionID
FROM user_question INNER JOIN question
ON user_question.question_id = question.question_id) as iResult
WHERE iResult = 'QUESTIONID HERE'
PS:请发布您的表格的SCHEMA
,以便我们知道确切的列名称以及您将在WHERE
条件中使用的字段。