2018-04-11 12:00:18 2261次浏览 1条回答 0 悬赏 50 金钱

做一个模糊查询,不想使用like实现,因为速度可能会比较慢,用locate实现会快点,但是这样写的话语法会报错

$res['data'] = ShowRecord::find()->where(['locate','name',$keyword])->all();
print_R($res['data']);die;

错误:
6.png

最佳答案

  • yzh52521 发布于 2018-04-11 13:17 举报
    $res['data'] = ShowRecord::find()->where("locate($keyword,'name')>0")->all();
    
    4 条回复
    回复于 2018-04-11 15:39 回复

    报错,

    SQLSTATE[42S22]: Column not found: 1054 Unknown column 't' in 'where clause'
    The SQL being executed was: SELECT * FROM `record` WHERE locate(t,'name')>0
    
    Error Info: Array
    (
        [0] => 42S22
        [1] => 1054
        [2] => Unknown column 't' in 'where clause'
    )
    
    回复于 2018-04-11 15:50 回复

    另外,如果是用where的操作符格式怎么显示呢 类似于 where(['like','name',$keyword])

    回复于 2018-04-11 15:58 回复

    $res['data'] = ShowRecord::find()->where("locate(‘$keyword’,'name')>0")->all();

    回复于 2018-04-11 17:00 回复

    这样还是不行,where("locate('".$keyword."',name)")这样就可以了,name不需要加引号,前面的变量需要解析才可以,不过能告诉这种方式怎么改为where的操作符模式来写么

    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
最爱大裤衩
见习主管

最爱大裤衩 上海

注册时间:2018-03-27
最后登录:2019-05-29
在线时长:8小时30分
  • 粉丝2
  • 金钱175
  • 威望0
  • 积分255

热门问题