2012-08-13 153 views
0

我有兩個表格:'比賽'和'條目'。在我的比賽桌上,我有一個「已完成」的欄目,告訴我比賽是否已經開始。在我的參賽桌上,我有一個用戶ID列和一個比賽ID列。我正在嘗試創建一個JOIN,通過檢查'entries'表中的所有行,然後檢查每個特定行是否能夠通過檢查相應的競爭ID是否在「競爭」表中完成,來查看用戶是否能夠競爭。我的加入並不是很強,而且迄今爲止我的輸球都有些失落。這是我迄今爲止,但它不能正常工作:Codeigniter/PHP MySQL加入

$this->db->select('*'); 
$this->db->from('entries'); 
$this->db->join('competitions', 'competitions.id = entries.competition_id'); 
$this->db->where('entries.user_id', $user_id); 
$this->db->where('competitions.complete', '0'); //0 for incomplete, 1 for complete 
$query = $this->db->get(); 
if ($query->num_rows() > 0) { 
    return FALSE; //user is currently in an active competition 
}else { 
    return TRUE; //user is not in an active competition 
} 

我目前笨工作,但在常規PHP的解釋就可以了也。非常感謝你的幫助!

+2

' $ this-> db-> join('competitions','competitions.id = entries.competition_id','left');' – 2012-08-13 23:03:56

+0

查詢的輸出是什麼?有沒有行?你能輸出發送給MySQL的實際查詢文本嗎? – gcochard 2012-08-13 23:04:10

+0

得到它的工作。這似乎是我的控制器和模型之間的問題。非常感謝你的幫助! – 2012-08-13 23:43:13

回答

0

您可以使用向左或向右加入更好的結果,您希望得到...

$this->db->join('table2','table1.id = table2.id','LEFT/RIGHT'); 

我thnk你也可以使用內,外連接...可能希望它是有用的