美文网首页php社区
mysql 多like查询和截取

mysql 多like查询和截取

作者: jtw | 来源:发表于2019-05-06 15:54 被阅读0次
SELECT
    id,
    NAME,
    sell_price,
    market_price,
    cost_price,
    create_time,
    weight
FROM
    goods
WHERE
    id IN (
        SELECT
            substring_index(
                substring_index(content, ',', 1),
                ':' ,- 1
            )
        FROM
            `log_operation`
        WHERE
            author LIKE '%yuny1%'
        OR author LIKE '%yuny2%'
        OR author LIKE '%yuny3%'
    );

表1:管理员操作日志表

CREATE TABLE `log_operation` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `author` varchar(80) NOT NULL COMMENT '操作人员',
  `action` varchar(200) NOT NULL COMMENT '动作',
  `content` text COMMENT '内容',
  `datetime` datetime NOT NULL COMMENT '时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='日志操作记录';

表二 商品表

CREATE TABLE `goods` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '商品ID',
  `name` varchar(60) NOT NULL COMMENT '商品名称',
  `goods_no` varchar(20) NOT NULL COMMENT '商品的货号',
  `model_id` int(11) unsigned NOT NULL COMMENT '模型ID',
  `sell_price` decimal(15,2) NOT NULL COMMENT '销售价格',
  `market_price` decimal(15,2) DEFAULT NULL COMMENT '市场价格',
  `cost_price` decimal(15,2) DEFAULT NULL COMMENT '成本价格',
  `up_time` datetime DEFAULT NULL COMMENT '上架时间',
  `down_time` datetime DEFAULT NULL COMMENT '下架时间',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `store_nums` int(11) NOT NULL DEFAULT '0' COMMENT '库存',
  `img` varchar(255) DEFAULT NULL COMMENT '原图',
  `ad_img` varchar(255) DEFAULT NULL COMMENT '宣传图',
  `is_del` tinyint(1) NOT NULL DEFAULT '0' COMMENT '商品状态 0正常 1已删除 2下架 3申请上架',
  `content` text COMMENT '商品描述',
  `keywords` varchar(255) DEFAULT NULL COMMENT 'SEO关键词',
  `description` varchar(255) DEFAULT NULL COMMENT 'SEO描述',
  `search_words` varchar(50) DEFAULT NULL COMMENT '产品搜索词库,逗号分隔',
  `weight` decimal(15,2) NOT NULL DEFAULT '0.00' COMMENT '重量',
  `point` int(11) NOT NULL DEFAULT '0' COMMENT '积分',
  `unit` varchar(10) DEFAULT NULL COMMENT '计件单位。如:件,箱,个',
  `brand_id` int(11) NOT NULL DEFAULT '0' COMMENT '品牌ID',
  `visit` int(11) NOT NULL DEFAULT '0' COMMENT '浏览次数',
  `favorite` int(11) NOT NULL DEFAULT '0' COMMENT '收藏次数',
  `sort` smallint(5) NOT NULL DEFAULT '99' COMMENT '排序',
  `spec_array` text COMMENT '序列化存储规格,key值为规则ID,value为此商品具有的规格值',
  `exp` int(11) NOT NULL DEFAULT '0' COMMENT '经验值',
  `comments` int(11) NOT NULL DEFAULT '0' COMMENT '评论次数',
  `sale` int(11) NOT NULL DEFAULT '0' COMMENT '销量',
  `grade` int(11) NOT NULL DEFAULT '0' COMMENT '评分总数',
  `seller_id` int(11) unsigned DEFAULT '0' COMMENT '卖家ID',
  `is_share` tinyint(1) NOT NULL DEFAULT '0' COMMENT '共享商品 0不共享 1共享',
  `is_delivery_fee` tinyint(1) NOT NULL DEFAULT '0' COMMENT '免运费 0收运费 1免运费',
  `_hash` int(11) unsigned NOT NULL COMMENT '预留散列字段',
  PRIMARY KEY (`id`,`_hash`),
  KEY `is_del` (`is_del`) USING BTREE,
  KEY `sort` (`sort`) USING BTREE,
  KEY `sale` (`sale`) USING BTREE,
  KEY `grade` (`grade`) USING BTREE,
  KEY `sell_price` (`sell_price`) USING BTREE,
  KEY `name` (`name`) USING BTREE,
  KEY `goods_no` (`goods_no`) USING BTREE,
  KEY `is_share` (`is_share`) USING BTREE,
  KEY `brand_id` (`brand_id`,`is_del`) USING BTREE,
  KEY `brand_id_2` (`brand_id`,`sell_price`) USING BTREE,
  KEY `brand_id_3` (`brand_id`,`grade`) USING BTREE,
  KEY `brand_id_4` (`brand_id`,`sale`) USING BTREE,
  KEY `store_nums` (`store_nums`,`is_del`) USING BTREE,
  KEY `seller_id` (`seller_id`,`is_del`) USING BTREE,
  KEY `seller_id_2` (`seller_id`,`sell_price`) USING BTREE,
  KEY `seller_id_3` (`seller_id`,`grade`) USING BTREE,
  KEY `seller_id_4` (`seller_id`,`sale`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=76618 DEFAULT CHARSET=utf8 COMMENT='商品信息表';


管理员操作日志信息

商品ID:1566,名称:小熊(Bear) 空气加湿器 家用迷你静音卧室办公室香薰机

需求 查询多管理员操作商品日志,截取该数据中的商品id,查出对应的商品。

使用 substring_index 截取 字符串

函数括号里面的依次为:要分隔截取的字符串(如:”aaa_bbb_ccc”)、分隔符(如:“_”)、位置(表示第几个分隔符处,如:“1”)。

count为正数,那么就是从左边开始数,函数返回第count个分隔符的左侧的字符串;

count为负数,那么就是从右边开始数,函数返回第count个分隔符右边的所有内容;

count可以为0,返回为空。

例子:substring_index("aaa_bbb_ccc","_",1) ,返回为 aaa;

substring_index("aaa_bbb_ccc","_",2) ,返回为 aaa_bbb;

substring_index(substring_index("aaa_bbb_ccc","_",-2),"_",1) ,返回为 bbb;

相关文章

网友评论

    本文标题:mysql 多like查询和截取

    本文链接:https://www.haomeiwen.com/subject/ukqdoqtx.html