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')
相关推荐
2301_781571422 小时前
Golang格式化输出占位符都有什么_Golang fmt占位符教程【通俗】
jvm·数据库·python
养肥胖虎2 小时前
RAG学习笔记(3):区分数据库检索与RAG的使用场景
数据库·ai·rag
_ku_ku_3 小时前
数据库系统原理 · 数据库应用开发 · 自学总结
数据库
No8g攻城狮3 小时前
【人大金仓】wsl2+ubuntu22.04安装人大金仓数据库V9
java·数据库·spring boot·非关系型数据库
山峰哥3 小时前
SQL慢查询调优实战:从全表扫描到索引覆盖的完整复盘
前端·数据库·sql·性能优化
代码中介商4 小时前
Redis入门:5大数据类型全解析
数据库·redis·缓存
渣渣盟4 小时前
数据库设计范式详解(纯小白版)
数据库·oracle·软考·数据库工程师
夜雪闻竹6 小时前
Cursor 对话导入:解析 SQLite 里的宝藏
数据库·sqlite·ai编程
hhb_6186 小时前
PL/SQL核心技术难点梳理与实战应用案例解析
数据库·sql
m0_470857646 小时前
PHP怎么实现工厂模式_Factory模式编写指南【指南】
jvm·数据库·python