【问题标题】:my sql query is not working and i couldnt know what went wrong here我的 sql 查询不起作用,我不知道这里出了什么问题
【发布时间】:2014-03-31 09:47:01
【问题描述】:
 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();
}

选择 * 来自 (workshops) 内连接workshop_brand ON workshops.id = workshop_brand.w_id 在哪里workshops.status = 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 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,5,2 ,2577') 或brand_id = '6' 分组workshops.id

【问题讨论】:

  • 你期望什么,应该查询和?有什么错误吗?
  • 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 8 行的“GROUP BY workshops.id”附近使用正确的语法

标签: mysql codeigniter


【解决方案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】:

    GROUP BY 应该在ORDER BY 之前使用,同样AND 1 没有用最好删除

     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'
    ) 
    

    使用活动记录,您可以这样做

    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(postcode,'" . $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.* ');
    }
    

    【讨论】:

    • 感谢您的建议,但很抱歉,但这仍然不起作用..加入我的查询中会产生任何问题..
    • 您能否定义不起作用什么不起作用您遇到的任何错误
    • 我正在使用 codeigniter 来运行这个查询。即使我在 orderby 之前放置 group by,它在打印 last_query 时也会给出相同的查询..
    • 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 8 行的“GROUP BY Workshop.id”附近使用正确的语法...................... ....这是错误
    • @Rozer 你能发布活动记录查询你是如何在你的问题中使用活动记录来尝试这个查询的
    【解决方案3】:

    谢谢你的建议.. 但是这段代码对我有用.. 非常感谢您的支持。

    $this->db->where("1 group by Workshop.id order by ", "FIND_IN_SET(postcode,'" . $postc . "')", false);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-29
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 2019-01-12
      相关资源
      最近更新 更多