Atualizado: Devemos usar prefere usar junções para um melhor desempenho quando for fácil de fazer para nós. Participar vs. subconsulta
Sql Fiddle
Select distinct Customer from orders o
join
(
SELECT distinct Customer as changedname FROM Orders o2
join
(
Select distinct invoice from Promotions where Coupon='couponA'
) t3
on o2.invoice = t3.invoice
) t2
on o.customer != t2.changedname;
Observação:alterei o nome da coluna customer para t3 porque duas tabelas unidas devem ter nomes de coluna diferentes
Explicação:
Usar consulta interna ou subconsulta é caro quando você tem big data. use joins, vamos aprender a converter subconsulta para join
Com Subconsulta Tivemos:
Select distinct Customer from orders where customer not in
(SELECT distinct Customer FROM Orders where invoice in
(Select distinct invoice from Promotions where Coupon='couponA'));
Convertendo a subconsulta para participar
Primeiro passo:
Select distinct Customer from orders o
join
(
SELECT distinct Customer as changedname FROM Orders where invoice in
(Select distinct invoice from Promotions where Coupon='couponA')
) t2
on o.customer != t2.changedname;
2º passo:
Select distinct Customer from orders o
join
(
SELECT distinct Customer as changedname FROM Orders o2 where invoice
join
(
Select distinct invoice from Promotions where Coupon='couponA'
) t3
on o2.invoice = t3.invoice
) t2
on o.customer != t2.changedname;
E pronto, muito mais rápido para tabelas com várias linhas
Resposta original:
Use
not in
. Dar uma olhada. Select distinct Customer from orders where customer not in
(SELECT distinct Customer FROM Orders where invoice in
(Select distinct invoice from Promotions where Coupon='couponA'));
Editar Eu adicionei distinto para tornar a consulta mais rápida