使用DISTINCT消除重复

时间:2015-01-04 07:36:43

标签: mysql

我有这3个。我在下面执行了查询,但查询为101返回5条记录,为201返回2条记录。

questions_tbl                   answers_tbl                 meter_tbl
 qid    qtype  other_columns     qid(fk)  other_columns      qtype      other_columns
  1     101                      1                           101
  2     201                      2                           101
  3     101                      3                           101
  4     101                      4                           201  

SELECT *
FROM answers, meter, questions
WHERE questions.qid=answers.question_id AND questions.subject_type = meter.subject_type

我也尝试过DISTINCT

SELECT DISTINCT
FROM answers, meter, questions
WHERE questions.qid=answers.question_id AND questions.subject_type = meter.subject_type  

1 个答案:

答案 0 :(得分:0)

像这样尝试DISTINCT。

 SELECT DISTINCT questions.qtype,other_column FROM answers, meter, questions
 WHERE questions.qid=answers.question_id AND questions.subject_type =  
 meter.subject_type   

使用GROUP BY

 SELECT * FROM answers, meter, questions
 WHERE questions.qid=answers.question_id AND questions.subject_type = meter.subject_type  
 GROUP BY questions.qtype