简体   繁体   English

如何从同一SQL中的两个不同表中获取两个值

[英]How to get two values from the two different tables in the same SQL

i have two tables 我有两张桌子

  • Questions 问题

    • Id_question Id_question
    • question
  • Answers 答案

    • Id_question Id_question
    • Id_answers Id_answers
    • answer 回答

If i use this query, 如果我使用此查询,

select q.question, a.answer from Question q, Answers a where q.Id_question= a.Id_question

i get the question and asociated answers but i repeat questions, for example 我得到了问题和相关的答案,但例如我重复了问题

question

First question?
  Yes
First question?
 No
Second question?
Probably
Second question?
 Yei

It´s posbile to get first question and asociated answers and then get the second question and asociated answers without repeat question? 是否有可能获得第一个问题和相关的答案,然后获得第二个问题和相关的答案而没有重复的问题?

It's not quite clear what you are looking for in the output, but you could try something like this: 尚不清楚输出中要查找的内容,但可以尝试如下操作:

select q.question, group_concat(a.answer)
from Question q, Answers a 
where q.Id_question= a.Id_question
group by q.question

the group_concat function will give you a comma-separated list. group_concat函数将为您提供一个逗号分隔的列表。 The output should look like: 输出应如下所示:

First question?  | Yes, No
Second question? | Probably, Yei

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM