如何在codeigniter中随机使用顺序

时间:2016-02-05 11:23:41

标签: codeigniter random sql-order-by

搜索时我想首先显示付费客户,然后列出其他客户

但问题是我想随机显示 例如。付费客户不应与他人混在一起。 谁能说出什么是查询?

请帮帮我! 我正在使用codeigniter

3 个答案:

答案 0 :(得分:2)

 Example:
 function randomval()
 {
      $this->db->order_by('id', 'RANDOM');
      $this->db->limit(1);
      $query = $this->db->get('tblname');
      return $query->result_array();

 }

答案 1 :(得分:1)

如果我理解的话,Mysql查询你的需要

SELECT `featured`,group_concat(`id` order by rand() ) as `id` FROM `dbc_posts` where `status` = 1 GROUP By `featured` ORDER BY `featured` DESC

现在用php

 $results  =  $this->db->query("SELECT `featured`,group_concat(`id` order by rand() ) as `id` FROM `dbc_posts` where `status` = 1 GROUP By `featured`  ORDER BY `featured` DESC")->result_array(); 

    $paid = $results[0];//featured = 1

    // comma seprated ids of the paid people e.g :- 3,7,1,26,92  are available in 
    $paidusers = $results[0]["id"];
    //seprate them by 
    $paidusers = explode(",",$paidusers);

    foreach($paidusers as $paiduser)
    {
        $row  =  $this->db->get_where("dbc_posts", array("id"=> $paiduser))->row();
        print_r($row );
        echo "<br>";
    }
     // do same for unpaid 
        $unpaid = $results[1];//featured = 0
    $unpaidusers = $results[1]["id"];
    //seprate them by 
    $unpaidusers = explode(",",$unpaidusers);

    foreach($unpaidusers as $unpaiduser)
    {
        $row  =  $this->db->get_where("dbc_posts", array("id"=> $unpaiduser))->row();
        print_r($row );
        echo "<br>";
    }

问我是否出现问题

答案 2 :(得分:1)

为数据库中的未付费客户设置为特征= 1为付费,特色为= 0。然后使用查询 mysql_query("SELECT * FROM dbc_posts WHERE status = 1 ORDER BY featured DESC, RAND() LIMIT 1");