I have 2 tables:
TABLE customer_service_provider
==================================
id | customer | service_provider
==================================
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
4 | 2 | 1
5 | 2 | 2
6 | 3 | 1
7 | 4 | 1
8 | 4 | 2
9 | 4 | 3
===================================
TABLE service_provider
======================
id | Name
======================
1 | Company1
2 | Company2
3 | Company3
======================
I need to get info from table customer_service_provider (fields customer and service_provider) which service_provider not exists in table customer_service_provider, but exist in service_provider table.
The result should look like:
customer | service_provider
==============================
2 | 3
3 | 2
3 | 3
==============================
SOLVED:
SELECT
DISTINCT sp.id,
csp.customer
FROM
service_provider sp,
customer_service_provider csp
WHERE
sp.id NOT IN( SELECT csp2.service_provider
FROM customer_service_provider csp2
WHERE csp2.customer = csp.customer)
Try this:
Or, more efficient:
I’ve not tested it, let me know.