This is the current structure of the database
Business login
A customer would be anyone in a predetermined list of customers that each company has information about. A profile is a login account they can use to access thier customer information.
When someone logs into their profile they can add one or more customer accounts to their profile as long as they have valid information about the customer (name,...)
It's not all that common but sometimes a customer will belong to multiple profiles.
customer :
+------------------+--------------------------+-----------+------------+-----+
| id (primary key) | company_id (primary key) | firstname | lastname | ... |
+------------------+--------------------------+-----------+------------+-----+
| 1 | 1 | John | Doe | ... |
+----------------------------------------------------------------------------+
| 2 | 1 | Jane | Doe | ... |
+----------------------------------------------------------------------------+
| 3 | 2 | Elmo | Mack | ... |
+----------------------------------------------------------------------------+
| 1 | 2 | Chester | Torain | ... |
+----------------------------------------------------------------------------+
profile :
+------------------+--------------------------+-----------+-------------+-----+
| id (primary key) | company_id (primary key) | username | email | ... |
+------------------+--------------------------+-----------+-------------+-----+
| 1 | 1 | odoe | j@gmail.com | ... |
+-----------------------------------------------------------------------------+
| 2 | 1 | adoe | d@gmail.com | ... |
+-----------------------------------------------------------------------------+
| 3 | 2 | emlo | e@gmail.com | ... |
+-----------------------------------------------------------------------------+
| 1 | 2 | ches | c@gmail.com | ... |
+-----------------------------------------------------------------------------+
(pivot) customer_profile :
+------------------+-------------+-------------+
| id (primary key) | customer_id | profile_id |
+------------------+-------------+-------------+
| 1 | 1 | 1 |
+----------------------------------------------+
| 2 | 1 | 2 |
+----------------------------------------------+
| 3 | 3 | 3 |
+----------------------------------------------+
There is a many to many relationship between customer and profile. A customer can have multiple profiles and each profile can be used to access one or many customers, hence the pivot table.
Both the customer and profile tables have composite keys of (id and company_id) that keep the records unique because of the way the data comes in.
There is also a Company Table
company :
+------------------+---------------+------+
| id (primary key) | company_name | ... |
+------------------+---------------+------+
| 1 | google | ... |
+-----------------------------------------+
| 2 | yahoo | ... |
+-----------------------------------------+
The company table has a one to many relation with the customer table and a one to many relationship with the profile table.
As a sidenote there are more tables in the database all with the same composite key structure.
Problem
If I do a normal query
SELECT
c.`firstname`
, c.`lastname`
, p.`username`
, p.`email`
FROM
`customer` c
LEFT JOIN `customer_profile` cp ON (cp.`customer_id` = c.`id`)
LEFT JOIN `profile` p ON (p.`id` = cp.`profile_id`)
WHERE c.`id` = 1 AND c.`company_id` = 1;
I would end up with incorrect relations where customer 1 company 1 (John Doe) would have records for his profile and for customer 1 company 2 (Chesters) profile because the pivot table doesn't account for the company id
The desired result would be the (firstname,lastname) of the customer and the (username,and email) of all profiles associated with that exact customer.
This is what the desired result would look be givin this data:
+-------------+------------+-----------+--------------+
| firstname | lastname | username | email |
+-------------+------------+-----------+--------------+
| John | Doe | odoe | j@gmail.com |
+-----------------------------------------------------+
| John | Doe | adoe | d@gmail.com |
+-----------------------------------------------------+
The actual result could contain additional records where the pivot table adds a relationship that shouldn't be.
But the actual result would also contain:
+-------------+------------+-----------+--------------+
| firstname | lastname | username | email |
+-------------+------------+-----------+--------------+
| John | Doe | odoe | j@gmail.com |
+-----------------------------------------------------+
| John | Doe | adoe | d@gmail.com |
+-----------------------------------------------------+
| John | Doe | ches | c@gmail.com | <--
+-----------------------------------------------------+
I would need to filter out any records like this where the profile and customer belong to different companies.
To fix this I have tried this query
SELECT
c.`firstname`
, c.`lastname`
, p.`username`
, p.`email`
FROM
`customer` c
LEFT JOIN `customer_profile` cp ON (cp.`customer_id` = c.`id`)
LEFT JOIN `profile` p ON (c.`company_id` = p.`company_id` AND p.`id` = cp.`profile_id`)
WHERE c.`id` = 1 AND c.`company_id` = 1;
which does give be the desired results because it joins by the company_id key as well This is what we are currently using I can't tell if it will work for all situations but it hasn't caused any unexpected results yet.
Question
My main problem is that I'm lost for what to do about the company table. If instead of what I currently have would joining the company table to the customer table be enough to remove any undesired records?
Something like this?
SELECT
c.`firstname`
, c.`lastname`
, p.`username`
, p.`email`
FROM
`company` co
LEFT JOIN `customer` c ON co.`id` = c.`company_id`
LEFT JOIN `customer_profile` cp ON (cp.`customer_id` = c.`id`)
LEFT JOIN `profile` p ON p.`id` = cp.`profile_id`
WHERE c.`id` = 1 AND c.company_id = 1;
It doesn't seem like it would because the pivot table doesn't have the company_id record.
Also any help finding another situation like mine that I can reference or take notes from would be very much appreciated.
Edit
Changed the queries where clause to search by customer.id instead of by firstname and changed the ' to ` This is how the wuery was supposed to be.
If the problem is in fact with the design of the database that's fine I just need to know. It would be simpler to use the current design but if that's where the problem is than that's that. From what I can tell from "philipxy" comment the problem is that there is no company_id field on the pivot table.
Edit
The logic behind the query is this
When this customer at this company (c.id AND c.company_id) has profiles with that company get the name and profile details.