SELECT c.*
,cc.*
,uc.*
FROM customers_companies cc
JOIN customers c
ON cc.customer_id = c.id
JOIN users_companies uc
ON cc.company_id = uc.id
WHERE uc.id = cc.company_id
Something like this?
Question
I tried some stuff but nothing seems to work, i will explain what it has to do,
First i have 3 mysql tables,
tablename: customers
All info of the customers(name, zipcode, etc)
tablename: customers_companies
Just a customer_id from table customers and company_id from table companies
tablename: users_companies
Just user_id from table users and company_id from table companies
First a user adds there company to the database, it adds user_id and company_id to users_companies
When you add a customer it adds the customer_id and company_id to customers_companies
It has to echo all customers from customers_companies in php but only if you part of the company so first mysql checks the customer_id and gets the company_id from customers_companies
With that company_id is search to all users who are allowed to see the customers in users_companies
When found all users that are allowed it have to get all information about the customers from table customers and echo it in a table in html/php
can someone help me? If you need more information just ask and i will answer
Thanks
EDIT:
<tbody>
<? $result = mysql_query("SELECT customers.* FROM `customers` LEFT JOIN `customers_companies` ON (`customer`.`id` = `customers_companies`.`company_id`) INNER JOIN `user_companies` (`customers_companies`.`company_id` = `customers_companies`.`company_id`) WHERE `customers_companies`.`bedrijf_id`");?>
<? $i = 1;?>
<? while($row = mysql_fetch_array($result)): ?>
<tr>
<td class="align-center"><? echo $i; ?></td>
<td> <?= $row['naam']; ?></td>
<td> <?= $row['adres']; ?></td>
<td> <?= $row['postcode']; ?></td>
<td> <?= $row['plaats']; ?></td>
<td> <?= $row['land']; ?></td>
<td> <?= 0,$row['telefoon']; ?></td>
<td>
<a href="#" class="table-icon edit" title="Edit"></a>
<a href="#" class="table-icon archive" title="Archive"></a>
<a onclick="return confirm('Weet je zeker dat je dit bedrijf wilt verwijderen? alle gekoppelde items worden ook verwijdert zoals facturen, klanten, enz')" href="user.php?p=bedrijven&del=<?= $row['id'];?>" class="table-icon delete" title="Delete"></a>
</td>
</tr>
<? $i += 1; ?>
<? endwhile; ?>
</tbody>
Solution
SELECT c.*
,cc.*
,uc.*
FROM customers_companies cc
JOIN customers c
ON cc.customer_id = c.id
JOIN users_companies uc
ON cc.company_id = uc.id
WHERE uc.id = cc.company_id
Something like this?