php - mysql one field by different priority ordering -
i have table having pay_mode field in subscription,onetime,free entries, want fetch record on subscription,onetime random order , free listing on descending order(not random) date.
$this->db->select("p.*", false); $this->db->from($this->table_classified." p"); $this->db->where('p.is_active', '1'); $this->db->where('p.is_deleted', 'no'); $this->db->order_by("(case `pay_mode` when 'recurring' 3 when 'onetime' 2 when 'free' 1 end)",'desc'); $this->db->order_by('pay_mode', 'random'); $query = $this->db->get();
please find schema here
you can write custom query on codeigniter , can fetch recurring , onetime random use union , fetch free listing desc order.
$query = "(select * (select nc_classified.*, nc_classified_category.title_zh cat_title_zh, nc_classified_category.title cat_title `nc_classified` left join `nc_classified_category` on nc_classified.cat_id = nc_classified_category.id ( $where ) , `pay_mode` in( 'recurring', 'onetime' ) order ( case `pay_mode` when 'recurring' 1 when 'onetime' 2 end ), rand()) a) union (select * (select nc_classified.*, nc_classified_category.title_zh cat_title_zh, nc_classified_category.title cat_title `nc_classified` left join `nc_classified_category` on nc_classified.cat_id = nc_classified_category.id ( $where ) , `pay_mode` in( 'free' ) order id desc) a) "; $cquery = $this->db->query($query);
Comments
Post a Comment