dongxing2015 2018-02-27 09:39
浏览 158
已采纳

从数据库中搜索关键字

function _searchProductByName($name = "") {
    $query = $this->db
            ->select("S.*,CC.id as catId, CC.name as CatName")
            ->from("categories as S")
            ->join("categories as C",'C.id=S.parentid','LEFT')
            ->join("categories as CC",'CC.id=C.parentid','LEFT')
            ->where("S.order", 2)
            ->like("S.name", $name, 'both')
            ->order_by("S.name", 'asc')
            //->order_by('locate('.$name.', S.name) asc, S.name asc')
            ->get();

    return $query && $query->num_rows() ? $query->result() : [];
}

when my keywords is lap it gives results:

{
"status": true,
"response": [
    {
        "id": "834",
        "parentid": "46",
        "name": "Collapsible Wardrobes",
        "order": "2",
        "hasChild": "0",
        "icon": null,
        "created": "2018-01-15 15:01:45",
        "catId": "5",
        "CatName": "Home Furniture, Decor , Pets"
    },
    {
        "id": "936",
        "parentid": "51",
        "name": "Laptop Desks",
        "order": "2",
        "hasChild": "0",
        "icon": null,
        "created": "2018-01-15 15:01:45",
        "catId": "5",
        "CatName": "Home Furniture, Decor , Pets"
    },
    {
        "id": "936",
        "parentid": "51",
        "name": "mobile latptop",
        "order": "2",
        "hasChild": "0",
        "icon": null,
        "created": "2018-01-15 15:01:45",
        "catId": "5",
        "CatName": "Home Furniture, Decor , Pets"
    }
  ]
}

when i use after in place or both it gives only Laptop Desks result but i need when i search for lap it gives me result.

Laptop Desk and mobile laptops

the first data is Collapsible Wardrobes which contains lap but it is in between collapsible which i don't need how do i get this?

  • 写回答

1条回答 默认 最新

  • dongtuo3530 2018-02-27 11:22
    关注

    If i understand you correctly (according to the comments) a possible solution could be to look for a word following with a space or at the beginning such as

    $query = $this->db
            ->select("S.*,CC.id as catId, CC.name as CatName")
            ->from("categories as S")
            ->join("categories as C",'C.id=S.parentid','LEFT')
            ->join("categories as CC",'CC.id=C.parentid','LEFT')
            ->where("S.order", 2)
            ->group_start()
                ->like('S.name', $name, 'after')
                ->or_like('S.name', ' '.$name, 'both')
            ->group_end()
            ->order_by("S.name", 'asc')
            //->order_by('locate('.$name.', S.name) asc, S.name asc')
            ->get();
    

    this should output

    SELECT `S`.*, `CC`.`id` AS `catId`, `CC`.`name` AS `CatName`
    FROM `categories` AS `S`
    LEFT JOIN `categories` AS `C` ON `C`.`id`=`S`.`parentid`
    LEFT JOIN `categories` AS `CC` ON `CC`.`id`=`C`.`parentid`
    WHERE `S`.`order` = 2 AND (`S`.`name` LIKE 'lap%' ESCAPE '!' OR `S`.`name` LIKE '% lap%' ESCAPE '!')
    ORDER BY `S`.`name` ASC
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助