mysql中INSTR函数的用法

mysql中INSTR函数的用法

INSTR(字段名, 字符串)

这个函数返回字符串在某一个字段的内容中的位置, 没有找到字符串返回0,否则返回位置(从1开始)

SELECT * FROM tblTopic ORDER BY INSTR( topicTitle, 'ha' ) > 0 DESC

SELECT INSTR( topicTitle, 'ha' ) FROM tblTopic

 

mysql中使用instr配合IN排序

将instr结果作为一列,按其排序

select id,1 from world_guide where id = 32
union
select * from
(select id, instr('30,35,31,',id+',') as d from
world_blog where id in (30,35,31) order by d) as t;

输出

+----+---+
| id | 1 |
+----+---+
| 32 | 1 |
| 30 | 1 |
| 35 | 4 |
| 31 | 7 |
+----+---+
4 rows in set, 6 warnings (0.02 sec)

 

 

表A 
字段:姓名 name
  张三
  王五
表B
字段:标题 title 
  信息一 张三发布
  信息二 王五发布
  信息三 张三发布

排行榜,按表A的姓名 like %‘name’% 匹配 表B的 title 的条数进行排序,

排行榜样例

张三 2
王五 1

select 姓名,count(b.title) from a inner join b on instr(b.title,a.姓名)>0
group by 姓名
order by count(b.title)
 

select name,(select count(*) from 表B where instr(title,表A.name)
from 表A
order by 2 desc

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值