2012-06-10 76 views
1

我需要從多個表中獲取一些用戶信息,但是如果缺少某些內容(如c.comp_title),例如查詢返回false。我怎樣才能讓它返回找到的任何數據?如果Mysql查詢未找到所有數據,則返回false

function one_edu($end_user, $one_id) 
    { 
    $query_str = "SELECT * 
       FROM edu a 
       JOIN user_profiles b ON a.user_id=b.user_id 
       JOIN (SELECT c.user_id, GROUP_CONCAT(c.comp_title) as comp_title, GROUP_CONCAT(c.comp) as comp 
        FROM comp c 
        GROUP BY c.user_id) c ON a.user_id = c.user_id 
       JOIN (SELECT s.user_id, GROUP_CONCAT(s.skill_title) as skill_title, GROUP_CONCAT(s.skill) as skill 
        FROM skills s 
        GROUP BY s.user_id) d ON a.user_id = d.user_id 
       JOIN (SELECT t.user_id, GROUP_CONCAT(t.exp_title) as exp_title, GROUP_CONCAT(t.experience) as experience 
        FROM exp t 
        GROUP BY t.user_id) e ON a.user_id = e.user_id 
       JOIN (SELECT e.user_id, GROUP_CONCAT(e.edu_title) as edu_title, GROUP_CONCAT(e.education) as education 
        FROM edu e 
        GROUP BY e.user_id) f ON a.user_id = f.user_id 
       WHERE a.user_id = ?"; 

      $query = $this->db->query($query_str, $end_user); 

      if($query->num_rows() > 0) 
        { 
        foreach($query->result_array() as $stuff) { 
          $data[] = $stuff; 
        } 
          return $data; 
        }else{ 
          return false; 
        } 

    }//end one_edu 

回答

1

瞭解有關SQL joins。您需要使用外部連接 - 即使要連接的表沒有匹配的記錄,也要使用外部連接 - 將JOIN替換爲LEFT JOIN,即使您想要結果,也要替換爲LEFT JOIN(或者,即使前面提到的表沒有匹配記錄,也要使用RIGHT JOIN)。

+0

謝謝。 LEFT JOIN完成了這個訣竅。非常有用的鏈接也。 – Ciprian

相關問題