sql按照指定数组顺序查询数据

1、常用sql语句分享
2、数据库SQL 某字段按首字母排序

使用FIND_IN_SET实现按顺序查询数据需求

  • sql如下:
java 复制代码
select `id`, `question` from `question_answer` where `id` in (16, 274, 15, 19) and `question_answer`.`deleted_at` is null order by FIND_IN_SET( 'id', ' 16, 274, 15, 19')
  • 在php中用法如下
java 复制代码
DB::table('user_question_click_log')->whereIn('id',$ques_ids )
            ->orderByRaw('FIND_IN_SET( id, "'.implode(',',$ques_ids). '" )')
            ->pluck('question')
相关推荐
沃尔威武2 小时前
数据库 Sinks(.net8)
数据库·.net·webview
Dreamboat¿3 小时前
SQL 注入漏洞
数据库·sql
曹牧4 小时前
Oracle数据库中,将JSON字符串转换为多行数据
数据库·oracle·json
被摘下的星星5 小时前
MySQL count()函数的用法
数据库·mysql
末央&5 小时前
【天机论坛】项目环境搭建和数据库设计
java·数据库
徒 花5 小时前
数据库知识复习07
数据库·作业
素玥5 小时前
实训5 python连接mysql数据库
数据库·python·mysql
jnrjian5 小时前
text index 查看index column index定义 index 刷新频率 index视图
数据库·oracle
瀚高PG实验室5 小时前
审计策略修改
网络·数据库·瀚高数据库
言慢行善6 小时前
sqlserver模糊查询问题
java·数据库·sqlserver