我遇到MySQL查询问题。
我有3个表:table1,table2和table3。
我想要做的是从table1获得一个明确的代码,其中日期是2013年6月,它的类型是“N”,而table3的状态从来都不是'确认'。
目前我收到包含“确认”状态的结果,因为可能有很多状态链接到代码。所以我想确保我的结果集永远不会包含'确认'。啰嗦,但我希望彻底。
这是我的问题:
select count(distinct(code))
from table1
where date between '2013-06-01' and '2013-07-01'
and type = 'N'
and id in (select id
from table2
where id_response in (select id_response
from table3
where status NOT LIKE 'Confirmation'));
我知道我的查询不是理想的,所以我欢迎所有的建议,以帮助我做得更好,并得到我需要的结果。
感谢您的帮助。
答案 0 :(得分:1)
我愿意:
select table1.code, count(table1.code)
from table1, table2, table3
where table1.date between '2013-06-01' and '2013-07-01'
and table1.type = 'N'
and table1.id = table2.id
and table2.id_response = table3.id_response
and table3.status <> 'Confirmation'
GROUP BY table1.code
答案 1 :(得分:0)
希望这会奏效:
select
table1.code,
count(table1.code)
from table1
INNER JOIN table2 on (table1.id = table2.id)
where table1.date between '2013-06-01' and '2013-07-01' and table1.type = 'N'
AND table2.id_response NOT IN
(Select table3.id_response from table3 WHERE table3.status = 'Confirmation')
GROUP BY table1.code