Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're looking for a full outer join. That way, you'll get all records from both tables, joined where possible. Unfortunately, MySQL doesn't support full outer joins, so here's a solution using unions and left and right outer joins.</p> <p>I have three tables, <code>companies</code>, <code>contacts</code>, and <code>companies_contacts</code>, where this final one is an association table. Here's the respective contents of the three:</p> <pre><code>mysql&gt; select * from companies; +------------+---------+ | company_id | company | +------------+---------+ | 1 | Foo | | 2 | Bar | | 3 | Baz | +------------+---------+ 3 rows in set (0.00 sec) mysql&gt; select * from contacts; +------------+---------+ | contact_id | contact | +------------+---------+ | 1 | Fred | | 2 | Barney | | 3 | Wilma | | 4 | Betty | +------------+---------+ 4 rows in set (0.00 sec) mysql&gt; select * from companies_contacts; +------------+------------+ | company_id | contact_id | +------------+------------+ | 1 | 1 | | 1 | 2 | | 2 | 2 | | 2 | 4 | +------------+------------+ 4 rows in set (0.00 sec) </code></pre> <p>The problem becomes simpler if you rethink it slightly: you want all the contacts that meet the criteria, along with the companies their associated with if possible, and you want all the companies that meet the criteria, with their associated contacts if possible. We can solve these two problems using two outer joins:</p> <pre><code>select company, contact from companies left join companies_contacts using (company_id) left join contacts using (contact_id) where company like '%B%'; </code></pre> <p>And:</p> <pre><code>select company, contact from companies right join companies_contacts using (company_id) right join contacts using (contact_id) where contact like '%W%'; </code></pre> <p>Using a union between these two queries will combine their results and eliminate any duplicates between the two:</p> <pre><code>select company, contact from companies left join companies_contacts using (company_id) left join contacts using (contact_id) where company like '%B%' union select company, contact from companies right join companies_contacts using (company_id) right join contacts using (contact_id) where contact like '%W%'; </code></pre> <p>On the previously mentioned data, this will give the result:</p> <pre><code>+---------+---------+ | company | contact | +---------+---------+ | Bar | Barney | | Bar | Betty | | Baz | NULL | | NULL | Wilma | +---------+---------+ </code></pre> <p>Which is exactly the result you're looking for.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload