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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM