title: MySQL Joining two unrelated tables
body: <p>On my website I have a form with a select field. On that field I give suggestions for the input with some ajax code. These suggestions are coming from a .json file I create dynamically on each key press.</p>
  
  <p>So this JSON file is filled with a single list. </p>
  
  <p><strong>JSON file:</strong></p>
  
  <pre><code>{
      "suggestions": [
          {
              "value": "Commandos: Behind Enemy Lines"
          },
          {
              "value": "Commandos 3: Destination Berlin"
          },
          {
              "value": "Commandos 2: Men of Courage"
          },
          {
              "value": "Commandos: Beyond the Call of Duty"
          }
      ]
  }
  </code></pre>
  
  <p>However the list is made up from two tables. These tables share no relation to each other. Both table got a 'gamename' which is just a VARCHAR column and I want those two into one big list. What would be the best (most efficient) way to get a result from them in one list?</p>
  
  <p><strong>Databases</strong></p>
  
  <blockquote>
    <p>Table 1 (giveaway)</p>
    
    <p>giveawayid | gamename</p>
    
    <p>-</p>
    
    <p>Table 2 (steam_app)</p>
    
    <p>appid | name | type</p>
  </blockquote>
  
  <p>What I have working now is doing 2 separate lookups to MySQL and then place all results into one PHP array. I also do a array_search on each result to prevent duplicate entries.</p>
  
  <pre><code>&lt;?php
  // query - BQ Game List
  $sql = "SELECT 
              gamename as value
          FROM giveaway
          WHERE gamename LIKE '$query%'
          GROUP BY gamename
          ORDER BY count(gamename) DESC
          LIMIT 50";
  $result = mysqli_query($dbConnection, $sql);
  
  if ($result-&gt;num_rows &gt; 0) {
      while ($row = mysqli_fetch_assoc($result)) {
          $json['suggestions'][] = $row;
          // create one array with all results to make it easier to use array_search()
          $haystack[] = $row['value'];
      }
  }
  
  // query - Steam App List
  $sql = "SELECT 
              name as value
          FROM steam_app
          WHERE name LIKE '$query%'
              AND (type = 'game'
                  OR type = 'dlc')
          ORDER BY name
          LIMIT 50";
  $result = mysqli_query($dbConnection, $sql);
  
  if ($result-&gt;num_rows &gt; 0) {
      while ($row = mysqli_fetch_assoc($result)) {
          // Check if value isn't already in the array
          if (array_search($row['value'], $haystack) === false) {
              $json['suggestions'][] = $row;
              // create one array with all results to make it easier to use array_search()
              $haystack[] = $row['value'];
          }
      }
  }
  
  // No results
  if (!isset($json)) {
      $json['suggestions'] = '';
  }
  
  // CLOSE CONNECTION
  mysqli_close($dbConnection);
  
  echo json_encode($json);
  ?&gt;
  </code></pre>
  
  <p>Now I wonder how efficient this is. Since doing two seperate lookups and lots of array_searches sounds to me like 3 resource intensive actions. Since this query gets called upon on every key press in the form field. I wonder if a single MySQL query wouldn't be more efficient. For instance by using a UNION.</p>
  
  <p>Any insight into this?</p>