MySQL中find_in_set的使用

Mysql 发表时间:2018-03-30 18:33:39 作者:梁子亮 浏览次数:834

FIND_IN_SET搜索的是以逗号分隔的字段,具体用法如下:

1、搜索时保持字段顺序不变

$order = "FIND_IN_SET(id,'1,3,15')";
$res = M('User')
    ->field('pid,nickname,img')
    ->where(['id'=>['in',$re_ids]])
    ->order($order)
    ->select();

2、搜索GoodsOrder表中tag_id为29或者32的数据,注意:tab_id字段储存格式为'1,4,29,33,'

$where['_string'] = "FIND_IN_SET(29,tag_id) OR FIND_IN_SET(32,tag_id)";//tag_id为表的某个字段
$casher = M("GoodsOrder")->where($where)->select();

3、tp5.1中的用法(all_shops为数据库中的字段(值为:1,3,16,22),aaa为随便起的变量名)

$where = new Where();
$where['roles'] = 'director';
$re = Db::name('admin')->where($where)->where('FIND_IN_SET(:aaa,all_shops)',['aaa' => 16])->find();