我在编写SQL查询时遇到问题。我有2个表:question和candidat_test_answer,格式如下。
问题:
(id_question) (id_catgorie)
| 1 |1
| 2 |1
| 4 |1
candidat_test_answer:
id_question || id_answer || id_test || id_candidat || date
| 1 || 2 || 17 || 1 ||2014-06-01
| 1 || 3 || 17 || 1 ||2014-06-01
| 2 || 1 || 17 || 1 ||2014-06-01
| 2 || 2 || 17 || 1 ||2014-06-01
| 1 || 2 || 17 || 2 ||2014-06-01
我想在结果中显示没有重复的记录。
id_question || id_test || id_candidat || date
| 1 || 17 || 1 ||2014-06-01
| 2 || 17 || 1 ||2014-06-01
这是我到目前为止所尝试的内容:
SELECT * FROM question q , candidat_test_reponse ctr where q.ID_Qt=ctr.ID_Qt and ctr.ID_Test=17 and ID_cand=1 and date='2014-06-01'
and q.ID_Qt NOT IN (SELECT q.ID_Qt FROM question q , candidat_test_reponse ctr where q.ID_Qt=ctr.ID_Qt and ctr.ID_Test=17 and ctr.ID_cand=1 and ctr.date='2014-06-01')
ORDER BY q.ID_Cat ,ctr.ID_Test DESC
但是,它目前返回0结果。我犯了什么错误?
答案 0 :(得分:0)
尝试此查询
select distinct question.id_question, candidat_test_answer.id_test,candidat_test_answer.id_candidat, date
from question join candidat_test_answer on question.id_question = candidat_test_answer.id_question
where ctr.ID_Test=17 and ID_cand=1 and date='2014-06-01'
答案 1 :(得分:0)
为什么加入问题表?你有什么内容,或者是什么?只需从table candidat_test_reponse中选择不同的行。
select distinct id_qt, id_test, id_cand, `date`
from candidat_test_reponse
where id_cand = 1 and `date` = '2014-06-01';
顺便说一句:你不应该使用 date 作为列名。它是SQL中的保留字。
答案 2 :(得分:0)
根据我的理解,只使用第二张表可以达到理想的效果。
尝试这样的事情: -
SELECT id_question, id_test, min(id_candidat), date
FROM vcandidat_test_answer
GROUP BY id_question, id_test, date;
希望这可以帮到你。