自学内容网 自学内容网

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

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

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

  • sql如下:
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中用法如下
DB::table('user_question_click_log')->whereIn('id',$ques_ids )
            ->orderByRaw('FIND_IN_SET( id, "'.implode(',',$ques_ids). '" )')
            ->pluck('question')

原文地址:https://blog.csdn.net/snow_love_xia/article/details/142762017

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!