我的SQL查询无法正常工作,我无法知道这里出了什么问题

时间:2014-03-31 09:47:02

标签: mysql codeigniter

 public function get_search($start = -0, $limit = null, $postcode = 'default',$branded) {


      if (!empty($branded)) {
            $this->db->join('workshop_brand', 'workshops.id = workshop_brand.w_id');
            $this->db->where('brand_id', $branded);
            $this->db->group_by("workshops.id");
        }
if(!empty($postcodes)){
$this->db->where('`postcode` in (' . $postcodes . ')');
                        $this->db->where("1 order by ", "FIND_IN_SET(`postcode`,'" . $postcodes . "')", false);
}

$query = $this->db->get('workshops', $limit, $start);
    echo $this->db->last_query();
   die();
        return $query->result_array();
}

SELECT *     FROM(workshops)     INNER JOIN workshop_brand ON workshopsid = workshop_brandw_id     在workshopsstatus = 1         和postcode IN(             2530             ,2500             ,2502             ,2505             ,2506             ,2517             ,2518             ,2519             ,2520             ,2522             ,2525             ,2526             ,2527             ,2528             ,2529             ,2533             ,2535             ,2560             ,2574             ,2575             ,2576             ,2577             )         和1     由FIND_IN_SET订购(postcode,'2530,2500,2502,2505,2506,2517,2518,2519,2520,2522,2525,2526,2527,2528,2529,2533,2535,2560,2574,2575 ,2576,2577' )         或brand_id ='6'     GROUP BY workshopsid

3 个答案:

答案 0 :(得分:1)

GROUP BY之前应该使用

ORDER BY,同时AND 1更好地删除<{p}}

 GROUP BY workshops.id   
 ORDER BY FIND_IN_SET(
  postcode,'2530,2500,2502,2505,2506,2517,2518,2519,2520,2522,2525,2526,2527,2528,2529,2533,2535,2560,2574,2575,2576,2577'
) 

使用Active记录,您可以通过

执行此操作
public function get_search($start = -0, $limit = null, $postcode = 'default', $branded)
{
    if (!empty($branded)) {
            $this->db->join('workshop_brand wb', 'w.id = wb.w_id');
            $this->db->where('wb.brand_id', $branded);
            $this->db->group_by("w.id");
        }
        if (!empty($postcodes)) {
            $this->db->where('`postcode` in (' . $postcodes . ')');
            $this->db->order_by("FIND_IN_SET(`postcode`,'" . $postcodes . "')", "asc" ,FALSE);
        }
             $this->db->select(' w.* ');
             $this->db->from(' workshops w ', $limit, $start);
             $this->db->limit($limit, $start);
             $query=$this->db->get();
    echo $this->db->last_query();
    die();
    return $query->result_array();
}

如果$this->db->order_by("FIND_IN_SET(邮编,'" . $postcodes . "')", "asc" ,FALSE);仍无效,请尝试

if(!empty($postcodes)){
    $this->db->select(" w.* ,FIND_IN_SET(`postcode`,'" . $postcodes . "') AS myorderby ",FALSE);
    $this->db->order_by("myorderby", "asc" ,FALSE);
}else{
    $this->db->select(' w.* ');
}

答案 1 :(得分:1)

您对GROUP BY和ORDER BY以及OR进行了错误排序。

这应该有效:

SELECT *
FROM (`workshops`)
INNER JOIN `workshop_brand` ON `workshops`.`id` = `workshop_brand`.`w_id`
WHERE (`workshops`.`status` = 1
AND `postcode` IN (
    2530
    ,2500
    ,2502
    ,2505
    ,2506
    ,2517
    ,2518
    ,2519
    ,2520
    ,2522
    ,2525
    ,2526
    ,2527
    ,2528
    ,2529
    ,2533
    ,2535
    ,2560
    ,2574
    ,2575
    ,2576
    ,2577
    ) )
 OR `brand_id` = '6'
 GROUP BY `workshops`.`id`
 ORDER BY FIND_IN_SET(`postcode`, '2530,2500,2502,2505,2506,2517,2518,2519,2520,2522,2525,2526,2527,2528,2529,2533,2535,2560,2574,2575,2576,2577')

答案 2 :(得分:0)

谢谢你的建议.. 但是这段代码对我有用了.. 真的很感谢你的支持。

$ this-&gt; db-&gt; where(“1 by workshop.id order by”,“FIND_IN_SET(postcode,'”。$ postc。“')”,false);