I have 3 tables I want to use for 1 SQL query.
customers:
- id
- first_name
- last_name
customer_professional:
- id
- customer_id
- professional_id
company_customer:
- id
- customer_id
- company_id
The query I'm trying to run is to grab all the customers where the professional_id = ? and the company_id = ?. This is the query I've come with it so far.
SELECT *
FROM customers c
INNER JOIN customer_professional cp
ON c.id = cp.customer_id
INNER JOIN company_customer cc
ON c.id = cc.customer_id
WHERE ( cp.professional_id = '36ec09ec-6b07-45e3-ae2d-a77bfe381bab' )
OR ( cc.company_id = '36ec09ec-6b07-45e3-ae2d-a77bfe381baa' )
ORDER BY "first_name"
LIMIT 50;
This query is returning only the customers that are in both the customer_professional and company_customer. The query I'm trying to run should return all customers even if they are only in 1 of the junction tables.
SIDE NOTE:
I know what you're thinking this should only be 2 tables. I agree with you.
UPDATE
Data I'm expecting:
customers table:
| id | first_name | last_name |
-------------------------------------
| 1 | peter | griffin |
| 2 | meg | griffin |
| 3 | stewie | griffin |
| 4 | louis | griffin |
| 5 | bryan | griffin |
customer_professional table:
| id | customer_id | professional_id |
--------------------------------------------
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 5 | 1 |
company_customer table:
| id | customer_id | company_id |
---------------------------------------
| 1 | 3 | 2 |
| 2 | 4 | 2 |
| 3 | 5 | 2 |
So with the query I want to run I have the company_id and the professional_id and I want to return the customers that belong to either a professional or the company.
query:
SELECT *
FROM customers c
INNER JOIN customer_professional cp
ON c.id = cp.customer_id
INNER JOIN company_customer cc
ON c.id = cc.customer_id
WHERE ( cp.professional_id = '1 )
OR ( cc.company_id = 2 )
ORDER BY "first_name"
LIMIT 50;
This only returns:
| id | first_name | last_name |
-------------------------------------
| 5 | bryan | griffin |
The query I'm trying to run should return
| id | first_name | last_name |
-------------------------------------
| 1 | peter | griffin |
| 2 | meg | griffin |
| 3 | stewie | griffin |
| 4 | louis | griffin |
| 5 | bryan | griffin |