title: How do I left join only if previous left join didn't find anything
body: <p>It shouldn't join on tbi2 if tbi1 finds a result, but it retrieving results from both tables.</p>
  
  <p>The idea of this is to join on the item table but if there isn't an exact match, fallback to joining on '*'</p>
  
  <p>(This is the same SQL statement as a previous question, but a different attempt... again.)</p>
  
  <pre><code> SELECT 
              COALESCE(tbi1.id,tbi2.id,tbi3.id,tbi4.id,tbi5.id,tbi6.id) AS id
              ,COALESCE(tbi1.cost,tbi2.cost,tbi3.cost,tbi4.cost,tbi5.cost,tbi6.cost) AS cost
              ,COALESCE(tbi1.price,tbi2.price,tbi3.price,tbi4.price,tbi5.price,tbi6.price) AS price 
              ,COALESCE(tbi1.itemNumber,tbi2.itemNumber,tbi3.itemNumber,tbi4.itemNumber,tbi5.itemNumber,tbi6.itemNumber) AS itemNumber
              ,COUNT(COALESCE(tbi1.id,tbi2.id,tbi3.id,tbi4.id,tbi5.id,tbi6.id)) AS quantityToAdd
              ,tbl_equipment.id AS equipId    
              ,tbl_equipment.partNum
              ,tbl_equipment.nolinks
              ,tbl_addedItemCustomerPrices.customerprice AS specialPrice
              ,tbl_testTypes.isInspt
              ,tbl_testTypes.isRecharge
              ,tbl_testTypes.isRecharagable
              ,tbl_tests.nextDate AS theNextDate
          FROM 
              tbl_tests
              INNER JOIN tbl_equipment ON tbl_equipment.id = tbl_tests.equipmentId
              INNER JOIN tbl_model ON tbl_model.id = tbl_equipment.model
              INNER JOIN tbl_testTypes ON tbl_testTypes.id = tbl_tests.testTypeId 
  
              LEFT JOIN tbl_items AS tbi1 ON tbi1.size = tbl_model.size AND tbi1.service = tbl_testTypes.name
              LEFT JOIN tbl_items AS tbi2 ON tbi2.size = '*' AND tbi2.service = tbl_testTypes.name AND tbi1.id IS NULL 
  
              LEFT JOIN tbl_items AS tbi3 ON tbi3.type = tbl_model.type AND tbi3.service = tbl_testTypes.name AND tbi2.id IS NULL 
              LEFT JOIN tbl_items AS tbi4 ON tbi4.type = '*' AND tbi4.service = tbl_testTypes.name AND tbi3.id IS NULL 
  
              LEFT JOIN tbl_items AS tbi5 ON tbi5.category = tbl_model.category AND tbi5.service = tbl_testTypes.name AND tbi4.id IS NULL 
              LEFT JOIN tbl_items AS tbi6 ON tbi6.category = '*' AND tbi6.service = tbl_testTypes.name AND tbi5.id IS NULL 
  
              LEFT JOIN tbl_addedItemCustomerPrices ON COALESCE(tbi1.id,tbi2.id,tbi3.id,tbi4.id,tbi5.id,tbi6.id) = tbl_addedItemCustomerPrices.itemId AND tbl_addedItemCustomerPrices.customerid = $currentSite 
          WHERE 
              ((nextDate &gt;= '$startDate' AND nextDate &lt;= '$endDate' ) OR tbl_testTypes.isRecharge = 'true')
              {$monthEndQuery}
              AND tbl_equipment.site = $currentSite 
          GROUP BY COALESCE(tbi1.id,tbi2.id,tbi3.id,tbi4.id,tbi5.id,tbi6.id)
          ORDER BY tbl_equipment.id, isRecharagable DESC
  </code></pre>