SELECT 子查询中的所有 EXCEPT 结果
SELECT all EXCEPT results in a subquery
我有一个连接多个表(3 或 4)的查询,并得到预期的结果。
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(-上面的查询 -)但是 returns 没有结果,这是不正确的。我也试过'NOT IN ( )'
有更好的方法吗?
尝试这样的事情:
(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
第二个查询到底是什么?我在这里只看到一个查询,没有子查询。此外,您的精确模式的 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
编辑:是的,原始问题中肯定缺少很多细节(在某些方面已得到修正),并且仍然缺少。了解示例的完整 table 结构在这里很重要,因此很难提供一个好的具体答案。
正如评论中所写,您应该可以这样做:
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
)
这是我的答案。 Left outer Join 给你参与者(测试)。如果 test_questions 中没有匹配项,那么它仍然是 return 测试行,但 test_questions 为空。因此,如果您随后查找任何 test_question.test_id 为 null,您将得到您要查找的内容。
我也会在您的计数子句中具体说明,而不是为了确保 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
我有一个连接多个表(3 或 4)的查询,并得到预期的结果。
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(-上面的查询 -)但是 returns 没有结果,这是不正确的。我也试过'NOT IN ( )'
有更好的方法吗?
尝试这样的事情:
(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
第二个查询到底是什么?我在这里只看到一个查询,没有子查询。此外,您的精确模式的 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
编辑:是的,原始问题中肯定缺少很多细节(在某些方面已得到修正),并且仍然缺少。了解示例的完整 table 结构在这里很重要,因此很难提供一个好的具体答案。
正如评论中所写,您应该可以这样做:
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
)
这是我的答案。 Left outer Join 给你参与者(测试)。如果 test_questions 中没有匹配项,那么它仍然是 return 测试行,但 test_questions 为空。因此,如果您随后查找任何 test_question.test_id 为 null,您将得到您要查找的内容。
我也会在您的计数子句中具体说明,而不是为了确保 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