Mysql按字段值CodeIgniter排序

时间:2014-05-16 14:36:35

标签: php mysql codeigniter

我想根据用户的性别取向和位置过滤数据库中的数据。

但是因为有些条目设置为针对所有性别,我希望结果显示在页面顶部最相关,然后不太相关。如果用户是女性,她应该看到第一个结果是女性目标,然后是其他结果。

  $data['sex'] = $this->model_user->getUserSex($user);
  $data['age'] = $this->model_user->getUserAge($user);
  $data['location'] = $this->model_user->getUserLoc($user);

  $where = "(target_sex = '".$data['sex']."' AND location='".$data['location']."') OR  (target_sex = 'all' AND location='".$data['location']."')";
        $this->db->where($where);
        $this->db->limit(3);
        $order = "CASE WHEN target_sex = '".$data['sex']."' THEN  location='".$data['location']."'";

        $this->db->order_by($order); 

        $sql = $this->db->get('toolkit_events');

        if($sql->num_rows() > 0){

            return $sql->result(); 

            }

它向我显示语法错误。

任何帮助都会非常棒。

0 个答案:

没有答案
相关问题