title: Php merge results of two different queries and remove duplicates
body: <p>I have two mysql tables. The first table <strong>theme_images</strong> contains default images for a given theme. The second table <strong>custom_theme_images</strong> contains custom images a user uploads to override a given default image in the themes table. </p>
  
  <p>The two tables are linked to each other using the <strong>them_image_id</strong> field in the custom_theme_images. In addition, each user is identified by a unique <strong>user_business_id</strong> because different users can override the same image in their own profile if they are using the same theme. Structure is as below:</p>
  
  <p><a href="https://i.stack.imgur.com/DZyjT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DZyjT.png" alt="Theme_images table"></a></p>
  
  <p><a href="https://i.stack.imgur.com/6gmN3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6gmN3.png" alt="custom_theme_images table"></a></p>
  
  <p>All I want is to be able to display all records in <strong>theme_images table</strong> with the default images but if a given user has overridden the image in the <strong>custom_theme_image</strong> table, then the custom image should be shown instead of the default image.</p>
  
  <p><strong>What I have tried</strong></p>
  
  <p>I have tried inner,left and right joins but they <strong>only return the fields that match</strong> in both tables only but I also want the defaults if a user has not overridden an image.</p>
  
  <p>I then decided to retrieve two different results from the two tables and I am trying to merge them  and remove duplicates. One result contains all custom images of a given profile and theme, then the second one contains all the defaults.
  Code is in Laravel style</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>&lt;tbody&gt;
      @php
          $tally=1;
      @endphp
      @foreach($custom_theme_image as $custom_image)
          &lt;tr&gt;
              &lt;td&gt;{{$custom_image-&gt;theme_image_id}}&lt;/td&gt;
              &lt;td&gt;{{$custom_image-&gt;theme_image-&gt;description ?? 'n/a'}}&lt;/td&gt;
              &lt;td&gt;
                  &lt;img src="storage/{{$custom_image-&gt;image}}" class="w-100 " style="max-width: 75px;"/&gt;
              &lt;/td&gt;
              &lt;td&gt; &lt;a href="/theme-images/{{$custom_image-&gt;theme_image_id}}/create"&gt;&lt;span class="badge bg-primary mr-2"&gt;Update&lt;/span&gt;&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
      @endforeach
      @foreach($theme_images as $theme_image)
          &lt;tr&gt;
              &lt;td&gt;{{$theme_image-&gt;id}}&lt;/td&gt;
              &lt;td&gt;{{$theme_image-&gt;description ?? 'n/a'}}&lt;/td&gt;
              &lt;td&gt;
                  &lt;img src="{{asset('previewThemes')}}/{{$theme_image-&gt;theme-&gt;name}}/{{$theme_image-&gt;image}}" class="w-100 " style="max-width: 75px;"/&gt;
              &lt;/td&gt;
              &lt;td&gt; &lt;a href="/theme-images/{{$theme_image-&gt;id}}/create"&gt;&lt;span class="badge bg-primary mr-2"&gt;Change&lt;/span&gt;&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
          @php
              $tally=$tally+1;
          @endphp
      @endforeach
  &lt;/tbody&gt;
  </code></pre>
  
  <p>The results of these two queries definitely have duplicates in them, i.e
  <a href="https://i.stack.imgur.com/szXwK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/szXwK.png" alt="Results of the two queries"></a></p>
  
  <p>The two foreach results: top results are the custom image ones, the bottom is the default image result.
  Now all I want is to be able to merge those duplicates to show the custom images but still retain the other default image records...
  Final IDs should read  3,4,5,6,7 ... with 3,4,5 showing custom image and 6,7 showing default images</p>
  
  <p>In this case:</p>
  
  <p><strong>user_business_id = 2<br/>
  theme_id = 2</strong></p>