查询颜色包含red和yellow的产品并且尺寸包含40和41
数据库存储格式:字段名attr_value 值 红色,黄色,40,41
方案1:
使用find_in_set
$attrArr = ['yellow', 'red', 40, 41]; // 这是前端传到后端的数据 $map = []; $sql_category = ""; foreach ($attrArr as $value) { $sql_category .= "FIND_IN_SET('{$value}',attr_value) and "; } $sql_category = rtrim($sql_category," and "); $map[] = ['', 'exp', Db::raw($sql_category)]; $list =Db::table("lonely_product_attr") ->where($map) ->select() ->toArray(); dump($list);
方案2:
使用like查询:
$attrArr = ['yellow', 'red', 40, 41]; // 这是前端传到后端的数据 $map = []; $sql_category = ""; foreach ($attrArr as $value) { $map[] = ['attr_value', 'like', '%'. $value .'%']; } $list =Db::table("lonely_product_attr") ->where($map) ->select() ->toArray(); dump($list);