2015-05-23 52 views
-2
$query=mysql_query("SELECT COUNT(*) as totalvotes FROM votes WHERE option_id IN(SELECT id FROM options WHERE ques_id='$poll_id')"); 

$query=mysql_query("SELECT options.id, options.value, COUNT(*) as votes FROM votes, options WHERE votes.option_id=options.id AND votes.option_id IN(SELECT id FROM options WHERE ques_id='$poll_id') GROUP BY votes.option_id"); 
+1

閱讀[docs](http://www.codeigniter.com/userguide3/database/queries.html)? – Tpojka

+0

您應該爲您的問題添加更多詳細信息,例如您正在嘗試做什麼,嘗試過什麼,以及您收到了什麼行爲。 – Goose

回答

0

假設你的查詢是否正確

$query=mysql_query("SELECT COUNT(*) as totalvotes FROM votes WHERE option_id IN(SELECT id FROM options WHERE ques_id='$poll_id')"); 

ANSWER

 $result = $this->db->select('count(*) AS totalvotes') 
       ->from('votes') 
       ->where("option_id IN(SELECT id FROM options WHERE ques_id='$poll_id')"); 

$query=mysql_query("SELECT options.id, options.value, COUNT(*) as votes FROM votes, options WHERE votes.option_id=options.id AND votes.option_id IN(SELECT id FROM options WHERE ques_id='$poll_id') GROUP BY votes.option_id"); 

ANSWER

 $result = $this->db->select('options.id, options.value, COUNT(*) as votes') 
       ->from('votes v') 
       ->join('options o', 'v.option_id=o.options.id') 
       ->where("v.option_id IN(SELECT id FROM v.options WHERE v.ques_id='$poll_id')") 
       ->group_by('v.option_id); 
+0

謝謝,一切都OK :) – lala

+0

@lala請檢查這個答案謝謝! –