Mysql 选择子查询中除结果之外的所有结果
我有一个连接多个表(3或4)的查询,并按预期获得结果Mysql 选择子查询中除结果之外的所有结果,mysql,subquery,not-exists,Mysql,Subquery,Not Exists,我有一个连接多个表(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 t
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
所以基本上我不想在第一个查询中包含第二个查询的结果。测试id将是连接字段
我尝试了一个WHERE NOT EXISTS(-上述查询-),但没有返回不正确的结果。我也试过‘不在()’
有更好的方法吗?试试这样的方法:
(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
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
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
)
第二个问题是什么?我在这里只看到一个查询,没有子查询。此外,精确模式的SQLFIDLE也会有所帮助 不管怎么说,我想你需要某种形式的左撇子。它看起来像这样:
(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
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
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
)
选择test.test\u id,count(*)作为计数
从测试
加入测试题tq
在tq.test\u id=test.test\u id上
左连接测试已排除。\u测试
在排除的测试上。测试id=测试。测试id
其中tq.type='THE_type'
和>
排除的测试id为空
编辑:是的,原来的问题中肯定有很多细节遗漏了(在某些方面已经被修复),现在仍然没有。了解示例的完整表结构在这里很重要,因此很难提供一个好的具体答案。如注释中所述,您应该能够这样做:
(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
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
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
)
这是我的答案。左外部联接为您提供了参与者(测试)。如果测试题中没有匹配项,那么它仍将返回测试行,但测试题为null。因此,如果您然后查找任何为空的test_question.test_id,您将得到您要查找的内容 我也会在yourcount子句中详细说明,而不是仅仅为了确保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
在没有看到您的实际查询的情况下,我冒昧地猜测您的子查询是不相关的。请发布您的真实查询、数据样本和预期结果。您的解释不太清楚,您的查询也不清楚:
test\u id=tq\u test\u id
-我们不知道这些列属于哪个表。您到底是如何尝试not IN()
?如果要将此查询用作子查询,则不需要结果集中的计数。@Philipp嗯,我无法使用此计数,因为计数/分组依据,所以我不得不放弃此想法。是的,但是如果要从其他结果集中排除数据,您需要计数做什么?你可以保留having子句,但不是在别名上,而是在实际计数(*)我只是想知道:test\u id
列是否存在于student\u tests
或student\u tests
中?我在什么地方漏掉了吗?不,它只是作为实际列的占位符,带有id,可以这样标记。OP暗示在子查询的select中只有count(*)一列打乱了他的查询我很确定这就是我所需要的。我不知道他的查询返回什么,但它的结构似乎在工作。我想上面的其他人可能也这么做了。当然,我在主查询中添加了“LEFT JOIN(ANSWER)”。以上只是我需要的相关部分。@Alex讽刺我猜?我要说,其他人似乎花了更多的时间在它和格式。但我想我会选择对我有用的答案。社区将投票选出我所期待的最好的。