Mysql:仅连接表左表的第一行

时间:2015-07-08 18:22:55

标签: mysql sql join

查询

SELECT * FROM Question 
INNER JOIN Question_Answer 
  ON Question.question_id = Question_Answer.question_id 
WHERE Question.question_group_id = 518

问题表包含以下列:

question_id|question_name|question_type|question_group_id|question_ask
1          |100-105      | gr           | 518                | question1
2          |100-105      | gr           | 518                | question2
3          |100-105      | gr           | 518                | question3
4          |100-105      | gr           | 518                | question4

Question_Answer包含以下列:

question_id|question_answer_text|question_answer_id|question_answer_is_correct
518           |Text1                    |1107                  |1
518           |Text2                    |1108                  |1
518           |Text3                    |1109                  |1
518           |Text4                    |1110                  |1

问题是输出的内容如下:

question_id|question_name|question_type|question_group_id|question_ask|question_id|question_answer_text|question_answer_id|question_answer_is_correct

1          |100-105      | gr           | 518                | question1 | 518           |Text1                    |1107                  |1
1          |100-105      | gr           | 518                | question1 | 518           |Text2                    |1108                  |1
1          |100-105      | gr           | 518                | question1 | 518           |Text3                    |1109                  |1
1          |100-105      | gr           | 518                | question1 | 518           |Text4                    |1110                  |1

我想要实现的目标是:

1          |100-105      | gr           | 518                | question1 | 518           |Text1                    |1107                  |1
2          |100-105      | gr           | 518                | question2 | 518           |Text2                    |1108                  |1
3          |100-105      | gr           | 518                | question3 | 518           |Text3                    |1109                  |1
4          |100-105      | gr           | 518                | question4 | 518           |Text4                    |1110                  |1

1 个答案:

答案 0 :(得分:0)

修复数据或修复您的加入。

您的加入是: Question.question_id = Question_Answer.question_id

但数据为: {1,2,3,4}<> {518}

连接错误或数据是。