mysql 字段包含某个字符的函数

  • 作者: 凯哥Java(公众号:凯哥Java)
  • 工作小总结
  • 时间:2019-01-22 09:26
  • 3715人已阅读
简介  通过sql查询语句,查询某个字段中包含特定字符串:例子:查询e_book表的types字段包含字符串"3",有下面4种方式select * from e_book where types like "%3%";select * from e_boo

🔔🔔好消息!好消息!🔔🔔

 如果您需要注册ChatGPT,想要升级ChatGPT4。凯哥可以代注册ChatGPT账号代升级ChatGPT4

有需要的朋友👉:微信号 kaigejava2022

 通过sql查询语句,查询某个字段中包含特定字符串:

例子:查询e_book表的types字段包含字符串"3",有下面4种方式

select * from e_book where types like "%3%";

select * from e_book where find_in_set('3', types);

select * from e_book where locate('3', types);


select * from e_book where INSTR(types,'3');

第2、3中方式相对速度较快

如果使用find_in_set不行的话,可以换成locate.

使用案例:

98a49bc8402555846c0170606776462a.png

select * from (
        select
                cb.id, cb.code, cb.contract_type contractType, cb.target,b.real_name targetName,
                cb.start_date startDate, cb.end_date endDate, cb.real_end_date realEndDate, cb.finance_group_id financeGroupId,
                cb.dept_name jobGroupName,
                cb.job_group_id jobGroupId,
                cb.dept_name_en deptNameEn ,
                cb.create_date createDate, cb.creater, cb.status,
                        cb.dept_code,
                        cb.dept_name_en,
                        cb.dept_name,
                        cb.product_line_code,
                        cb.product_line_name,
                        cb.subject_code,
                cb.subject_name,
                    temp.name as tname,
                     case  when locate('劳务',  temp.name) then '1'
            when locate('服务',  temp.name) then '2'
                end as newType
                from cb_contract cb
                left join base b on cb.target = b.id
                        left join contract_template temp on cb.contract_template = temp.id

                where cb.is_delete = 1    
         
            and dept_code in
             (  
                'DEP0009'
             ) 
         
         ) cb

        where 1=1
        and (
         (newType = '2'
         
            and product_line_code in
             (  
                'PL0092'
             ) 
         
         
        )
        or (newType = '2'  and ( product_line_code is null or product_line_code = ''))
        or (newType = '1' )
        )

        order by cb.id desc
        limit 0, 10


TopTop