网站制作学习网→http://www.forasp.cn/→正文:mysql order 自定义排序 thinkphp order 自定义排序指定顺序
字体:

mysql order 自定义排序 thinkphp order 自定义排序指定顺序

http://www.forasp.cn/ 2022/7/14 20:59:23  点击:不统计


在做数据查询的时候,遇到了固定排序,在thinkphp模版中也一样指定顺序,自定义顺序,那么怎么做到mysql自定义排序呢

 

看mysql的数据

id   title

1 mysql

2 自定义

3 指定

4 排序

5 order

6 模板

7 顺序

8 thinkphp

看mysql 查询指定顺序查询,实现自定义排序

我们要求排序 1,3,4  ,8 ,6, 5

即结果要求为

1 mysql

3 指定

4 排序

8 thinkphp

6 模板

5 order

 

首先看原生 sql 实现 指定顺序查询

select id,title from news where id in (1,3,4,8,6,5) order by FIELD(id,1,3,4,8,6,5) 

如果在thinkphp 下实现 指定顺序或者自定义顺序

'where'=>" id  in (1,3,4,8,6,5 ) asc"

'orderstr'=>" FIELD(id,1,3,4,8,6,5) asc"


·上一篇:utf8mb4_general_ci utf8mb4_bin 区别 >>    ·下一篇:mysql 导出表结构 >>
推荐文章
最新文章