ITeye博客

让sql语句不排序,按照in语句的顺序返回结果

liuwang126 · 2011-12-23 17:54 · mysql · 数据库 · Markdown

mysql:

SELECT * FROM EVENT WHERE eventId IN(443,419,431,440,420,414,509)  ORDER BY INSTR(',443,419,431,440,420,414,509,',CONCAT(',',eventId,','))

 

oracle:

select name from order where oderid in(111,222,333,444,555,666) order by instr('111,222,333,444,555,666',orderid)

 

 

补充:

order by 2 desc

在mysql 中instr函数的语法是:INSTR(字段名, 字符串)。这个函数返回字符串在某一个字段的内容中的地位, 没有找到字符串返回0,不然返回地点的地位(是从1开端)。

SELECT * FROM file ORDER BY INSTR( Title, 'au' ) > 0 DESC

 

SELECT INSTR( title, 'ha' ) FROM file

 

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;

 

from  表A 

表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)

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。