2016-06-29 37 views
1

我有一個連接多個表(3或4)的查詢並按預期得到結果。在子查詢中選擇所有EXCEPT結果

SELECT DISTINCT test_title, stt_id FROM student_tests 
LEFT JOIN student_test_answers ON sta_stt_num = stt_id 
JOIN tests ON stt_test_id = test_id 
WHERE student_test_answer_id IS NULL 

我有另外一個查詢,顯示另一組數據,它基本上是這樣的:

SELECT test_id, COUNT(*) AS theCount FROM tests 
    JOIN test_questions ON test_id= tq_test_id 
    WHERE type= 'THE_TYPE' 
    GROUP BY test_id 
    HAVING theCount = 1 

所以基本上我想不包括第一個第二個查詢的結果。 test_id將是連接字段。

我試過了WHERE NOT EXISTS( - 上面的查詢 - )但是沒有返回任何不正確的結果。我也試過'NOT IN()'

有沒有更好的方法來做到這一點?

+0

沒有看到您的實際查詢,我敢說,你的子查詢是不相關的。 – mustaccio

+0

請發表您的真實問題,數據樣本和預期結果。你的解釋不是很清楚,你的查詢也不清楚:'test_id = tq_test_id' - 我們不知道那些列屬於哪個表 – Alex

+1

你究竟是如何嘗試'NOT IN()'的?如果您想將此查詢用作子查詢,則不需要結果集中的theCount。 – Philipp

回答

3

嘗試這樣:

(SELECT test_id, COUNT(*) AS theCount FROM tests 
JOIN test_questions ON test_id= tq_test_id 
WHERE type= 'THE_TYPE' 
GROUP BY test_id 
HAVING theCount = 1) outer 
LEFT JOIN (
     [OtherQuery] 
) a ON outer.test_id = a.test_id 
WHERE a.test_id IS NULL 
+0

我很確定這是我所需要的。 – KickingLettuce

+0

我不知道他的查詢返回什麼,但它的結構似乎正在工作。我認爲上面的其他人可能也是這樣做的。當然,我將它添加爲我的主要查詢中的「LEFT JOIN(ANSWER)」。以上只是我需要的相關部分。 – KickingLettuce

+0

@Alex諷刺我猜?我會說其他人似乎把更多的時間和格式化。但我想我會選擇適合我的答案。社區將爲我所尋找的最好的一個提供贊助。 – KickingLettuce

0

到底是什麼第二個查詢?我只在這裏看到一個查詢,並且沒有子查詢。另外,精確模式的sqlfiddle會很有幫助。

無論如何,我想你想要某種排除連接。它看起來是這樣的:

select  test.test_id, count(*) as theCount 
from  tests test 
join  test_questions tq 
on   tq.test_id = test.test_id 
left join tests excluded_test 
on   excluded_test.test_id = test.test_id 
where  tq.type = 'THE_TYPE' 
and  << Some condition that explains what excluded_test is >> 
and  excluded_test.test_id is null 

編輯:是啊,肯定有很多細節,從原來的問題失蹤(其中有在某些方面被修補),以及這些人仍下落不明。知道你的例子的完整表格結構很重要,所以很難提供一個好的具體答案。

1

寫在評論你應該能夠做到這一點是這樣的:

SELECT 
    DISTINCT test_title, 
    olc_stt_i_num 
FROM 
    student_tests 
LEFT JOIN 
    student_test_answers 
     ON olc_sta_i_stt_num = olc_stt_i_num 
INNER JOIN 
    ol_class_tests 
     ON stt_test_num = test_num 
WHERE 
    student_test_answer_id IS NULL 

    -- added this: replace test_id with real column 
    AND ***test_id*** NOT IN (
     SELECT 
     test_id 
     FROM 
     tests  
     JOIN 
     test_questions 
      ON test_id= tq_test_id  
     WHERE 
     type= 'THE_TYPE'  
     GROUP BY 
     test_id  
     HAVING 
     COUNT(*) = 1 
    ) 
+1

我只是想知道:hwo你知道'test_id'列存在於'student_tests'或'student_test_answers'或'ol_class_tests'嗎?我錯過了什麼地方? – Alex

+0

不,它只是作爲帶ID的實際列的佔位符,將其標記爲這樣。 OP暗示有這樣的列,因爲只有在子查詢選擇中的計數(*)正在搞亂他的查詢 – Philipp

1

這裏就是我的回答。左外連接爲您提供參與者(測試)。如果test_questions中沒有匹配,則它將返回測試行,但對於test_questions則爲null。因此,如果您查找任何空的test_question.test_id,您將得到您正在查找的內容。

我也會在你的count子句中具體說明,而不是爲了確保mysql知道你真正想要計數的數值(*)。

create database test; 
use test; 

create table test 
(
    test_id int, 
    `the_type` varchar(20) 
); 

create table test_questions 
(
    test_question_id int, 
    test_id int, 
    `the_type` varchar(20) 
); 

insert into test values (1, 'history'); 
insert into test values (2, 'chemistry'); 
insert into test values (3, 'reading'); 

insert into test_questions values (1, 1, 'hard question'); 
insert into test_questions values (2, 1, 'medium question'); 
insert into test_questions values (3, 2, 'hard question'); 
insert into test_questions values (4, 2, 'easy question'); 

select * from test; 
select * from test_questions; 

select t.test_id, count(distinct t.test_id) 
from test t 
left outer join test_questions tq on tq.test_id = t.test_id 
where 
    tq.test_id is null 
group by 
    t.test_id