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')
相关推荐
pixcarp1 天前
知识库系统的内容资产闭环怎么设计
服务器·数据库·后端·golang
JosieBook1 天前
【数据库】时序预测能力的分级进化:TimechoAI如何让每一类用户都能精准预见未来
java·开发语言·数据库
吴声子夜歌1 天前
SQL经典实例——使用多张表
数据库·sql
倔强的石头_1 天前
《Kingbase护城河》——深度解密数据库行锁冲突与等待事件架构
数据库
IT策士1 天前
Redis 从入门到精通:性能调优与多语言客户端对比
数据库·redis·缓存
Bert.Cai1 天前
Oracle INSTR函数详解
数据库·oracle
茉莉玫瑰花茶1 天前
综合案例 - AI 智能租房助手 [ 5 ]
服务器·数据库·人工智能·python·ai
ywl4708120871 天前
jwt生产token,简单版helloworld
java·数据库·spring
器灵科技1 天前
AI视频工具实测:Seedance/可灵/HappyHorse谁最能打?
java·运维·数据库·人工智能·github
摇滚侠1 天前
MyBatis 入门到项目实战 特殊 SQL 的执行 34-37
java·sql·mybatis