我有一个名为Questions
的表2000 questions
,我使用MySql
数据库和Codeigniter
,我想要的是select 10 PERCENT questions of total 2000 with a query
in笨
答案 0 :(得分:1)
在控制器中
public function FunctionName()
{
$result = $this>model_name->get_question();
print_r($result);
}
在模型中
public function get_question()
{
#get all question count
$query= $this->db->query("SELECT * FROM question");
$result = $query->result_array();
$count = count($result); # returns 2000
$limit = ($count/100)*10; # returns 200
# select only limited question which set with $limit
$query= $this->db->query("SELECT * FROM question LIMIT $limit");
$result = $query->result_array();
return $result
}